1

I have a program where I have a settings button. This button has an image icon which shows an image. Any tips on how I can preform an action when it is pressed. Here is that code

JButton imageButton = new JButton(new ImageIcon("/Users/Sam/Programming/Files/gears.png"));

It works fine. But when I want to use

else if(ae.getActionCommand().equals(imageButton)){//doStuff}

it doesnt work. My action preformed method works, here it is

public void actionPerformed(ActionEvent ae)
{}
Sam Liokumovich
  • 91
  • 1
  • 2
  • 6
  • getActionCommand() returns a String not an Object. http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionEvent.html#getActionCommand() –  Aug 31 '15 at 00:33

1 Answers1

0

Your ActionListerner is not responding because getActionCommand() should return a String. In this case it should be the name of the JButton.

 else if(ae.getActionCommand().equals(imageButton.getText())){//doStuff}

Changing it to imageButton.getText() should solve the problem.

If you are not sure, just manually set the ActionCommand through setActionCommand() and use the same String to verify getActionCommand in the .equals() method.