Using ActionListener to determine value in JComboBox

B

Ben

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.
 
B

Ben

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.
 
M

Monique Y. Mudama

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.
 
V

Vova Reznik

Ben said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top