简体   繁体   中英

Java Event Listeners

I'm trying to separate components of my Java desktop app into different classes. For example, I have a MenuBar class, called from the MainClass, that creates the JMenuBar.

Normally, I would implement ActionListener in the MenuBar class and override actionPerformed() to keep everything organized. But if I do that, how can I let the MainClass know what was clicked?

I tried implementing my own ActionListener, but I couldn't come up with a solution that was capable of dispatching events to other classes.

MainClass.java

public class MainClass extends JFrame {

     private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(false);
        JFrame frame = new JFrame("Main Window");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MenuBar menuBar = new MenuBar();
        JMenuBar mb = menuBar.createMenu();
        frame.setJMenuBar(mb);

        frame.setSize(400,400);
        frame.setVisible(true);

     }

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

}

MenuBar.java

public class MenuBar implements ActionListener {

     public JMenuBar createMenu() {

         JMenu menu;
         JMenuItem item;

         JMenuBar menuBar = new JMenuBar();

         menu = new JMenu("Main");
         menuBar.add(menu);

         item = new JMenuItem("New");
         menu.add(item);

         return menuBar;

   }

   @Override
   public void actionPerformed(ActionEvent e) {
         JMenuItem source = (JMenuItem)(e.getSource());
         System.out.println("Action triggered on: "+source.getText());
         // *** Let MainClass know what was clicked ??
   }

}

EDIT

I realized I can just create an ActionListener in the MainClass and pass that to the MenuBar class, like this:

Amended MainClass.java

ActionListener l = new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
     System.out.println(ae.getActionCommand());
   }
};

MenuBar menuBar = new MenuBar();
frame.setJMenuBar(menuBar.createMenu(l));

And then, in MenuBar, I just apply the ActionListener to each of the menu items.

Amended MenuBar.java

public JMenuBar createMenu(ActionListener l) {

    item = new JMenuItem("Hide When Minimized");
    item.addActionListener(l);
    menu.add(item);

}

One possible solution is MainClass implements ActionListener and pass its instance to MenuBar.createMenu():

public class MenuBar {

   public JMenuBar createMenu( ActionListener l ) {
      ...
      menuItem.addActionListener( l );
   }
   ...
}

MainClass side:

public class MainClass extends JFrame {

   @Override
   public void actionPerformed(ActionEvent e) {
      Object source = e.getSource();
      System.out.println( "Action triggered by: " + source );
   }

   private static void createAndShowGUI() {
      ...
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      MenuBar menuBar = new MenuBar();
      JMenuBar mb = menuBar.createMenu( frame );
      frame.setJMenuBar( mb );
      ...
   }
}

Another way is to use java.beans.PropertyChangeListener and java.beans.PropertyChangeSupport

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM