Identifying the source of event when using ActionListener

M

Michael

I have a Swing application with numerous buttons and checkboxes... and
I have trouble identifying the source of the events. How can I check
which combobox triggered the event?

There is this e.getSource() function but I don't know how to utilise
it, since if(e.getSource().equals(<ComboBoxName>)does't seem to work.

How can I solve this problem?


MRV
 
M

Michael Cox

set action command on the components then

if(e.getActionCommand.equals("thecommand"))
{
// etc..
}else if (e.....("anothercommand")){
// ..
}
 
M

Michael Cox

Alternatively,
you could use anonymous inner classes ..

e.g.

button.addActionListener(new ActionListener()
{
public void
actionPerformed(ActionEvent ae)
{
// do what u want it to do here
}
});

Michael Cox
 
S

Sam

I have a Swing application with numerous buttons and checkboxes... and
I have trouble identifying the source of the events. How can I check
which combobox triggered the event?

There is this e.getSource() function but I don't know how to utilise
it, since if(e.getSource().equals(<ComboBoxName>)does't seem to work.

How can I solve this problem?


MRV

Just a thought - the getSource returns an object reference, so that
wouldn't be the same as the combobox name. What about casting the
result of getSource to ComboBox and calling getName on that, and
comparing it?

Regards,
Sam90
 
R

RobertMaas

From: (e-mail address removed) (Michael)
if(e.getSource().equals(<ComboBoxName>)

At the point where you did new ComboBox, you have a reference to that
actual object, right? So if you saved that somewhere, for example
fooRef, later can you test if(e.getSource()==fooRef) ?
(I don't have access to any java GUI from home so I can't check that to
see if it really works before posting, sorry.)

Do you click on the ComboBox, so *you* know which one it was, but you
can't get the program to know that from any conditional code? Or your
GUI application is just sitting there and you don't click on anything
and from out of nowhere an event is signalled and you have no idea what
caused that?

If *you* know which one it was, can you make a copy of your program and
delete everything unrelated to that one ComboBox, to create a program
that will fit on one or two screens so you can post it and we can look
at it and try it ourselves to see why it's not working?

But if the event seems to come from nowhere, so not even *you* know
what caused it, you can comment out all the registrations of action
listeners, which should cause such an event to never occur again, then
comment some of them back in until you find which one is causing it.
Then at that point *you* know which one it was, so you can now make a
tiny program per the previous paragraph.
 
M

Michael

Guys, thanks for your help, I solved the problem by setting the Action Command.


Mike
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top