Welcome to Dream.In.Code
Become a Java Expert!

Join 149,598 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,898 people online right now. Registration is fast and FREE... Join Now!




JComboBox together JButton

 
Reply to this topicStart new topic

JComboBox together JButton

erus
5 Oct, 2007 - 07:34 AM
Post #1

New D.I.C Head
*

Joined: 21 Oct, 2006
Posts: 3


My Contributions
hi guys.I have been writing a program but i faced a problem when i use ComboBox and JButton together.How can i use both of them in ActionListener,that is ,when i click the button and choose a ComboBox item together ,there will be a message on the dos prompt like "two action will be chosen"..
Thanks...
User is offlineProfile CardPM
+Quote Post

Programmist
RE: JComboBox Together JButton
5 Oct, 2007 - 08:45 AM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
You want to detect what JComboBox item is selected when you click the JButton?
User is offlineProfile CardPM
+Quote Post

erus
RE: JComboBox Together JButton
5 Oct, 2007 - 09:25 AM
Post #3

New D.I.C Head
*

Joined: 21 Oct, 2006
Posts: 3


My Contributions
QUOTE(Programmist @ 5 Oct, 2007 - 09:45 AM) *

You want to detect what JComboBox item is selected when you click the JButton?


i have a jcombobox containing two item like("x","y") and a jbutton.at first i choose x item then click jbutton
and then a meesage will be displayed on dos prompt.if i choose y item and then click jbutton there will be another message.What my problem is that.Thanks.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: JComboBox Together JButton
5 Oct, 2007 - 11:05 AM
Post #4

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Easy. Your JButton should have a registered ActionListener that detects mouse clicks. That listener should check the value of the JComboBox. Try it.
User is offlineProfile CardPM
+Quote Post

erus
RE: JComboBox Together JButton
5 Oct, 2007 - 12:47 PM
Post #5

New D.I.C Head
*

Joined: 21 Oct, 2006
Posts: 3


My Contributions
QUOTE(Programmist @ 5 Oct, 2007 - 12:05 PM) *

Easy. Your JButton should have a registered ActionListener that detects mouse clicks. That listener should check the value of the JComboBox. Try it.


But how?Here is one of the codes that i tried.But that is not work correctly.

jcb.addActionListener(this);
jb.addActionListener(this);
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("button"))
{

jcb =(JComboBox)e.getSource();

String m=(String)jcb.getSelectedItem();

if(m.equals("x") ) {

System.out.println("vs...");}

if(m.equals("y")){

System.out.println("......"); }
}
}

This post has been edited by erus: 5 Oct, 2007 - 12:50 PM
User is offlineProfile CardPM
+Quote Post

Programmist
RE: JComboBox Together JButton
8 Oct, 2007 - 06:34 AM
Post #6

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
http://java.sun.com/docs/books/tutorial/ui...onlistener.html
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month