|
From: Ben on 11 Feb 2006 17:02 Hi, Could anyone help me with this problem. I have an action listener associated with a number of buttons. I can get the action listener to work, but what code do I need to findout which button was pressed. This is the layout of the action listener class. import java.awt.event.*; import java.awt.Component; public class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { } } Thanks in advance. Ben.
From: Chris Lamb on 11 Feb 2006 17:21 On 11 Feb 2006 14:02:14 -0800, Ben wrote: > Hi, > Could anyone help me with this problem. I have an action listener > associated with a number of buttons. I can get the action listener to > work, but what code do I need to findout which button was pressed. > > This is the layout of the action listener class. > > import java.awt.event.*; > import java.awt.Component; > > public class ButtonListener implements ActionListener > { > public void actionPerformed (ActionEvent e) > { > > } > > } > > Thanks in advance. > > Ben. Have you tried setActionCommand() method for the JButton? Then you can use e.getActionCommand() in your ActionListener to query it. Chris
From: Thomas Hawtin on 11 Feb 2006 19:14 Chris Lamb wrote: > On 11 Feb 2006 14:02:14 -0800, Ben wrote: > >> Could anyone help me with this problem. I have an action listener >> associated with a number of buttons. I can get the action listener to >> work, but what code do I need to findout which button was pressed. >> >> This is the layout of the action listener class. >> >> import java.awt.event.*; >> import java.awt.Component; >> >> public class ButtonListener implements ActionListener >> { >> public void actionPerformed (ActionEvent e) >> { >> >> } >> >> } > > Have you tried setActionCommand() method for the JButton? Then you can use > e.getActionCommand() in your ActionListener to query it. Or more directly, use e.getSource(). It's usually better to create a new listener instance for each event source. Tom Hawtin -- Unemployed English Java programmer http://jroller.com/page/tackline/
From: Chris Lamb on 11 Feb 2006 19:10 On Sun, 12 Feb 2006 00:14:30 +0000, Thomas Hawtin wrote: > Chris Lamb wrote: >> On 11 Feb 2006 14:02:14 -0800, Ben wrote: >> >>> Could anyone help me with this problem. I have an action listener >>> associated with a number of buttons. I can get the action listener to >>> work, but what code do I need to findout which button was pressed. >>> >>> This is the layout of the action listener class. >>> >>> import java.awt.event.*; >>> import java.awt.Component; >>> >>> public class ButtonListener implements ActionListener >>> { >>> public void actionPerformed (ActionEvent e) >>> { >>> >>> } >>> >>> } >> >> Have you tried setActionCommand() method for the JButton? Then you can use >> e.getActionCommand() in your ActionListener to query it. > > Or more directly, use e.getSource(). > > It's usually better to create a new listener instance for each event source. Ah, yes indeedy. I'm afraid I must put my hand up and admit to using the 'cheap 'n' nasty' on more occasions than is appropriate :). Chris
From: Knute Johnson on 11 Feb 2006 19:27 Thomas Hawtin wrote: >> >> Have you tried setActionCommand() method for the JButton? Then you can >> use >> e.getActionCommand() in your ActionListener to query it. > > Or more directly, use e.getSource(). > > It's usually better to create a new listener instance for each event > source. > > Tom Hawtin I don't know, for one or two buttons maybe. But if you have a bunch of menus and buttons I find it to be much less confusing to have all of the events go to the same listener and separate them out there. -- Knute Johnson email s/nospam/knute/
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: max heap size on 32 bit linux ? Next: Code examples for job interview |