Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Using ActionListener to determine value in JComboBox

Thread view: 
Ben - 13 Feb 2006 19:45 GMT
Hi, can anyone help with this?

At the moment I have a class that implements ActionListener and
overides the method actionPerformed (ActionEvent e).
         I have a number of JButtons that have the custom listener
added to them using addActionListener(customListener).  In order to
find out which button was pressed I attach an action command to them
using setActionCommand("text").  I then use getActionCommand() in the
custom listener class to find out which button was pressed.  This works
great for buttons.  But I have a JComboBox that contains a number of
values.  How can I do a similar thing and find out what value was
selected in the combobox from the listener?

Thanks in advance for any help.

Ben.
Ben - 13 Feb 2006 20:34 GMT
Don't worry I got it I used getSource and cast it to a JComboBox.
Ben - 13 Feb 2006 20:46 GMT
Actually thinking again is there any way to use ActionEvent.getSouce()
and distinguish if a JButton or JComboBox were used and then which
button and what value in combobox without using setActionCommand?

Thanks.

Ben.
Monique Y. Mudama - 13 Feb 2006 20:56 GMT
> Actually thinking again is there any way to use
> ActionEvent.getSouce() and distinguish if a JButton or JComboBox
> were used and then which button and what value in combobox without
> using setActionCommand?

It sounds like you're looking for the instanceof operator.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Roedy Green - 13 Feb 2006 22:04 GMT
On Mon, 13 Feb 2006 13:56:40 -0700, "Monique Y. Mudama"
<spam@bounceswoosh.org> wrote, quoted or indirectly quoted someone who
said :

>It sounds like you're looking for the instanceof operator.

see http://mindprod.com/jgloss/instanceof.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Vova Reznik - 13 Feb 2006 21:41 GMT
> Actually thinking again is there any way to use ActionEvent.getSouce()
> and distinguish if a JButton or JComboBox were used and then which
[quoted text clipped - 3 lines]
>
> Ben.

Yes, source instanceof JComboBox for example.

But, what if you will have more than one JComboBox?

If you started mark your buttons by setActionCommand(String)
you may continue it with JComboBox too.
Or you may set to any Component name as identifier


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.