取消 JComboBox 操作事件

发布于 2024-12-28 04:24:29 字数 281 浏览 1 评论 0原文

我正在编写一个 gui 程序,并且有一个用于打开文件的 Jbutton 的 AbstractAction。在 JComboBox 中,我有一个已打开的文件列表。 JComboBox 的 AbstractAction 将更改回任何已打开的文件。当我更新 JComboBox 的列表时,该操作会触发。

因此,当我实际打开文件时,JComboBox 操作会触发,而当我使用 JComboBox 时,操作会触发一次,然后在更新时触发第二次。

有没有办法在更新 JComboBox 列表时停止该事件?

提前致谢

I'm writing a gui program and have an AbstractAction for a Jbutton that opens a file. In a JComboBox I have a list of the files that have been opened. The AbstractAction for the JComboBox will change back to any of the files that have been opened. When I update the list for a JComboBox though the action fires.

So when I actually open a file the JComboBox action fires, and when I use the JComboBox the action fires once, then a second time when updating.

Is there a way i can stop the event when just updating the JComboBox list?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错々过的事 2025-01-04 04:24:29

答案在于设计,特别是关注点的分离:不要考虑具有两个操作的视图,而是考虑多个视图更改单个数据的状态。

在伪代码中,类似于:

// data class
public class MyOpenFilesBean {

    private File currentFile; 

    public void setCurrentFile(File current) {
         File old = getCurrentFile();
         this.currentFile = current;
         firePropertyChange("currentFile", old, getCurrentFile());
    }

    public File getCurrentFile() {
        return currentFile;
    }

}  

// view wiring (view --> data)

Action open = new AbstractAction(...) {

      public void actionPerformed(...) {
          File choosenFile = // grab it from whereever in the view
          myOpenFileBean.setCurrentFile(choosenFile);
      }  

};
myButton.setAction(open);
myComboBox.setAction(open);

// view wiring (data --> view)

PropertyChangeListener l = new PropertyChangeListener() {
     public void propertyChanged(...) {
         if ("currentFile".equals(evt.getPropertyName()) {
               // a method implemented to update f.i. the combo selection  
               updateView((File) evt.getNewValue());
         }
     } 
};
myOpenFileBean.addPropertyChangeListener(l);

The answer is in the design, particularly in separation of concers: don't think view-with-two-actions, instead think many-views-change-state-of-single-data.

In pseudo-code something like:

// data class
public class MyOpenFilesBean {

    private File currentFile; 

    public void setCurrentFile(File current) {
         File old = getCurrentFile();
         this.currentFile = current;
         firePropertyChange("currentFile", old, getCurrentFile());
    }

    public File getCurrentFile() {
        return currentFile;
    }

}  

// view wiring (view --> data)

Action open = new AbstractAction(...) {

      public void actionPerformed(...) {
          File choosenFile = // grab it from whereever in the view
          myOpenFileBean.setCurrentFile(choosenFile);
      }  

};
myButton.setAction(open);
myComboBox.setAction(open);

// view wiring (data --> view)

PropertyChangeListener l = new PropertyChangeListener() {
     public void propertyChanged(...) {
         if ("currentFile".equals(evt.getPropertyName()) {
               // a method implemented to update f.i. the combo selection  
               updateView((File) evt.getNewValue());
         }
     } 
};
myOpenFileBean.addPropertyChangeListener(l);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文