From: Ben on
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
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
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
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
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/