简体   繁体   中英

JButton Action Listener from another Class

Im working with JButton events. I have a JPanel class, let's call Panel1, containing a public JButton, let's call it Button1. When this button is clicked:

//Inside Panel1
Button1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e)
   {
      System.out.println("1")
   }
});

From another JPanel Class, let's call it Panel2, that contains the Panel1, i have to handle the event "Button1 Pressed".

//Inside Panel2
Panel1.Button1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e)
   {
      System.out.println("2")
   }
});

The result obtained is:

2
1

But I'm interested to have:

1
2

Any suggestion?

If you add ActionListeners to a JButton, you cannot guarantee the order with which they will fire, and know that the order of addition is no guarantee to help. One way around this is to use the ActionListener to change the state of the object, and then listen for that. This will guarantee that the ActionListener fires first.

For example using a PropertyChangeListener as the 2nd listener:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

public class ActionOrder extends JPanel {
   ButtonPanel buttonPanel = new ButtonPanel();
   OtherPanel otherPanel = new OtherPanel();

   public ActionOrder() {
      add(buttonPanel);
      add(otherPanel);

      buttonPanel.addPropertyChangeListener(ButtonPanel.PRESSED, new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent evt) {
            otherPanel.appendText("Button 1 Pressed");
         }
      });
   }

   private static void createAndShowGui() {
      ActionOrder mainPanel = new ActionOrder();

      JFrame frame = new JFrame("ActionOrder");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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


class ButtonPanel extends JPanel {
   public static final String PRESSED = "pressed";
   private JButton button1 = new JButton("Button 1");

   public ButtonPanel() {
      add(button1);
      button1.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            System.out.println("1");
            firePropertyChange(PRESSED, null, PRESSED);
         }
      });

      setBorder(BorderFactory.createTitledBorder("Button Panel"));
   }
}

class OtherPanel extends JPanel {
   private JTextArea textArea = new JTextArea(10, 20);

   public OtherPanel() {
      add(new JScrollPane(textArea));
      setBorder(BorderFactory.createTitledBorder("Other Panel"));
   }

   public void appendText(String text) {
      textArea.append(text + "\n");
      System.out.println("2");
      System.out.println();
   }
}

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