3
import java.awt.*;

public class TestButton {
  private Frame f;
  protected Button b;

  public TestButton() {
    f = new Frame("Test");
    b = new Button("Press Me!");
    b.setActionCommand("ButtonPressed");
  }

  public void launchFrame() {
    b.addActionListener(new ButtonHandler());
    f.add(b, BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);
  }

  public static void main(String args[]) {
    TestButton guiApp = new TestButton();
    guiApp.launchFrame();
  }
}

import java.awt.*;
import java.awt.event.*;

public class ButtonHandler extends TestButton implements ActionListener {
  public void actionPerformed(ActionEvent e) {
      Object source = e.getSource();
      if(source==b)
      {
            System.out.println("Action occurred");
            System.out.println("Button's command is: "
                               + e.getActionCommand());
      }
  }
}

I'm trying to invoke a ActionEvent when the button b is pressed but not working with getSource.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Karlx Swanovski
  • 2,869
  • 9
  • 34
  • 67

3 Answers3

8

You're misusing inheritance. The ButtonHandler class should not extend the TestButton class, since the b variable in the handler class refers to a completely different Button object from the one displayed. I suggest:

  • Use the Swing library, not the AWT library
  • You can get the JButton pressed from the ActionEvent's getSource() method and use it directly.
  • If you need a reference to the GUI in the handler, pass in a reference in the handler's constructor.
  • Don't misuse inheritance to solve problems that don't involve inheritance issues.

For example:

import java.awt.event.ActionEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class TestButton extends JPanel {
   private JButton btn = new JButton(new ButtonAction("Press Me!", "ButtonPressed"));

   public TestButton() {
      add(btn);
   }

   private static void createAndShowGUI() {
      TestButton testButton = new TestButton();

      JFrame frame = new JFrame("TestButton");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(testButton  );
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}

@SuppressWarnings("serial")
class ButtonAction extends AbstractAction {
   public ButtonAction(String name, String actionCommand) {
      super(name);
      putValue(ACTION_COMMAND_KEY, actionCommand);
   }

   @Override
   public void actionPerformed(ActionEvent evt) {
      System.out.println("Button's actionCommand is: " + evt.getActionCommand());
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

That happens because you extends TestButton in your ButtonHandler, because of you have 2 different instances of your Button and thea are not equals.

To fix that you can remove extends TestButton and make ButtonHandler as inner class of TestButton

or you can compare action commands instead of Button's like next:

  if(((Button)source).getActionCommand().equals("ButtonPressed"))
alex2410
  • 10,904
  • 3
  • 25
  • 41
0

I think you need to remove extends TestButton as the 2 different instances of the buttons are not equal. You should go for a ButtonHandler as inner class or anonymous class to implement this.

Check this question: Java Button Handler

Community
  • 1
  • 1
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118