Question

I have a main class in a program that launches another class that handles all the GUI stuff. In the GUI, i have a button that i need to attach an ActionListener to.

The only problem is, the code to be executed needs to reside within the main class.

How can i get the ActionPerformed() method to execute in the main class when a button is clicked elsewhere?

Was it helpful?

Solution

Make your controller ("main" class) implement the ActionListener interface, then pass a reference to the view class:

public class View extends JFrame {
  public View(final ActionListener listener) {
   JButton button = new JButton("click me");
   button.addActionListener(listener);
   button.setActionCommand("do_stuff");

   getContentPane().add(button);

   pack();
   setVisible(true);
  }
 }

 public class Control implements ActionListener {

  public Control() {
   new View(this);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
   if (e.getActionCommand().equals("do_stuff")) {
    // respond to button click
   }
  }
 }

It can also be done with Actions, but that's more useful where you want one piece of code to respond to many buttons.

OTHER TIPS

Implement an anonymous inner class as ActionListener on the button, then call the method on your main class. This creates less dependencies and avoids the tag & switch style programming that implementing the ActionListener interface on a main class tends to promote.

In either case it will create a cycle in your dependency graph: the main class will know about the button and the button will need to call the main class. This might not be a good idea since it will make it hard to compose things in any other way. But without further information it is hard to judge the situation or recommend anything concrete.

Implement ActionListener in your main class and add the main class instance as a listener on your GUI button.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top