Home arrow static arrow Java Programming [Archive] - Event fired from which object
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Event fired from which object
This topic has 3 replies on 1 page.

Posts:8
Registered: 7/11/02
Event fired from which object  
Aug 6, 2004 9:45 AM



 
Hi,

I have a combo box and a button and I've registered event listeners on both of them like this:

button.addActionListener(this);
combobox.addActionListener(this);

I'd like to be able to listen for events separately on these two objects, can someone supply me with some sample code?

Thanks,
Paul
 

Posts:24,036
Registered: 2/3/03
Re: Event fired from which object  
Aug 6, 2004 9:48 AM (reply 1 of 3)



 
public void actionPerformed(ActionEvent evt) {    if (evt.getSource() == button) {        // it was the button    }    if (evt.getSource() == combobox) {        // it was the combobox    }}


[url=http://java.sun.com/docs/books/tutorial/uiswing/]Trail: Creating a GUI with JFC/Swing[/url]
 

Posts:19,725
Registered: 9/26/01
Re: Event fired from which object  
Aug 6, 2004 9:48 AM (reply 2 of 3)



 
Then create a different object which implements the required interface (ActionListener), and pass a reference to that object instead of 'this'.
 

Posts:8
Registered: 7/11/02
Re: Event fired from which object  
Aug 6, 2004 10:04 AM (reply 3 of 3)



 
That worked.

Thanks!
Paul
 
This topic has 3 replies on 1 page.