首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >取消JComboBox actionEvent

取消JComboBox actionEvent
EN

Stack Overflow用户
提问于 2012-01-19 13:00:59
回答 1查看 514关注 0票数 0

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

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

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

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-19 19:40:18

答案就在设计中,特别是在分割者的设计中:不要认为有两个动作的视图,而是考虑多个视图更改单个数据的状态。

在伪代码中类似于:

代码语言:javascript
复制
// 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);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8921242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档