java - 将事件从自定义组件传递到父组件

标签 java swing

我有一个扩展 JComponent 的自定义组件。当您单击它时,它会使用 MouseListener 捕获事件。根据组件的状态和您单击的位置,其中存储的数据会更新。

发生这种情况时如何通知父容器,同时保持其完全独立?

最佳答案

我自己,我会使用 JComponent 附带的 PropertyChangeSupport,然后在程序状态发生变化时触发通知方法。

例如:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

public class PropChangeEg {
   private static void createAndShowGui() {
      final JLabel counterLabel = new JLabel(" ", SwingConstants.CENTER);

      CustomComponent myCustomComponent = new CustomComponent();
      myCustomComponent.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent pcEvt) {
            if (pcEvt.getPropertyName().equals(CustomComponent.COUNTER)) {
               String text = "Counter: " + pcEvt.getNewValue();
               counterLabel.setText(text);
            }
         }
      });

      JFrame frame = new JFrame("PropChangeEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(myCustomComponent, BorderLayout.CENTER);
      frame.add(counterLabel, BorderLayout.PAGE_END);
      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 CustomComponent extends JComponent {
   public static final String COUNTER = "counter";
   private int counter = 0;

   public CustomComponent() {
      setLayout(new FlowLayout());
      add(new JButton(new AbstractAction("Increment Counter") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            setCounter(counter + 1);
         }
      }));
      add(new JButton(new AbstractAction("Decrement Counter") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            setCounter(counter - 1);
         }
      }));
   }

   //@Override // not needed!!
   //public void addPropertyChangeListener(PropertyChangeListener listener) {
   //   super.addPropertyChangeListener(listener);
   //}

   //@Override // not needed!!
   //public void removePropertyChangeListener(PropertyChangeListener listener) {
   //   super.removePropertyChangeListener(listener);
   //}

   public void setCounter(int counter) {
      int oldValue = this.counter;
      int newValue = counter;
      this.counter = newValue;
      firePropertyChange(COUNTER, oldValue, newValue);
   }

   public int getCounter() {
      return counter;
   }


}

关于评论:

ChangeListener 也可以正常工作。 1+ 到 user129...但我喜欢 PCL 的是,我可以指定要监听的属性名称,以便让我监听多个不同状态的变化,这种方式可以从 MVC 设计创建完整的 View ,即与控制完全解耦。所以我的建议是:如果您只监听一种状态,那么请务必使用 ChangeListener,但如果您正在监听多个状态,请使用 PCL。

关于java - 将事件从自定义组件传递到父组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10337539/

相关文章:

java - Spring Data MongoDB : Database name must only contain letters, 数字、下划线和破折号

java - 更改 JFrame 的内容很慢,还是我做错了?

java - 如何在 Java Swing 中对单击执行多个操作

java - 使用 Scribe 在 Android 中无法工作的情况下向 LinkedIn 发送消息

java - 我无法让我的 2d 游戏对象移动

java - 从 jTextArea 上的 shell 命令获取结果

java - 与文件相关的 NullPointerException

java - 如何在spring MVC中获取应用程序路径

Java 动态 Jtable

java - 提取数组元素以分离数组