Results 1 to 2 of 2
  1. #1
    baltimore is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with Handler

    Hi, My question is if I have two buttons:
    Java Code:
    JButton buttonOne = new JButton("ADD");
    buttonOne.addActionListener(this);
    JButton two = new JButton("ADD");
    buttonTwo.addActionListener(this);
    How do I make it so that the add button associates with buttonOne does one thing and the add button associates with buttonTwo does another thing?
    I have tried the following but both buttons does the same thing when I click on it:

    Java Code:
    e.getActionCommand().equals("ADD");
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    If you do not explicitly set the actionCommand for a JButton the getActionCommand method will return the button text. This can be a problem when the app is run in a different language. You can avoid this by setting an actionCommand for each button; even if you set the same string as the button text it will be preserved. For your situation you can set different strings for the actionCommand of each button and look for them in the handler.
    Java Code:
    JButton buttonOne = new JButton("ADD");
    buttonOne.setActionCommand("unique_string");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •