Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ActionHandler problem

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
hi.
my problem is not specifically code based. I just want to understand how each Component in Swing & awt (e.g. JButton) can have specific code to execute.

I know a-bit about events and have got all that working, I just don't know how I'm going to differentiate between a user clicking button1 and button2.

They will both send an ActionEvent object to the ActionListener class which then executes the actionPerformed() method, but i want both to do something different! and at the moment they both do the same thing!

Is there a way of identifying a specific and immutable reference when an ActionEvent object is sent which (using 'case' and 'if...else' statements, to differentiate)

Thankyou for all ur help!
Oxy we are all of us living in the gutter.
But some of us are looking at the stars.
 
In the actionPerformed method, you can use event.getSource to find out which button was pressed...

...
JButton JButtonDone = new JButton();
JButton JButtonUpdate = new JButton();
...
public void actionPerformed( java.awt.event.ActionEvent event )
{
Object object = event.getSource();
if( object == JButtonDone )
{
JButtonDone_actionPerformed( event );
}
else if( object == JButtonUpdate )
{
JButtonUpdate_actionPerformed( event );
}
}
 
Another way is to do something similar with the
Code:
getActionCommand()
method
Code:
...
JButton JButtonDone = new JButton();
JButtonDone.setActionCommand("Done"); // by default the label that appears on the button
                                      // is the action command, but sometimes this 
                                      //can be too long to be useful.
JButton JButtonUpdate = new JButton();
JButtonUpdate.setActionCommand("Update");
...
   public void actionPerformed( java.awt.event.ActionEvent event )
   {
      String actionCommand = event.getActionCommand();
      if( action.equals("Done") )
      {
         JButtonDone_actionPerformed( event );
      }
      else if( actionCommand.equals("Update") )
      {
         JButtonUpdate_actionPerformed( event );
      }
   }
-------------------------------------------
There are no onions, only magic
-------------------------------------------
 
Or, add a different actionListener class to each button.

If your using an anonomous class, create a different method call for each, i.e....

button1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e) { button1_actionPerformed(e); }
});

button2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e) { button2_actionPerformed(e); }
});


void button1_actionPerformed(ActionEvent e)
{
// .....
}

void button2_actionPerformed(ActionEvent e)
{
// .....
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top