我正在写一个gui程序,并且有一个打开文件的Jbutton的AbstractAction。在JComboBox中,我有一个已经打开的文件的列表。JComboBox的AbstractAction将更改回已打开的任何文件。但是,当我更新JComboBox的列表时,将触发该操作。
因此,当我实际打开一个文件时,JComboBox操作将触发,当我使用JComboBox时,该操作将触发一次,然后在更新时触发第二次。
有没有办法在更新JComboBox列表时停止事件?
提前感谢
发布于 2012-01-19 19:40:18
答案就在设计中,特别是在分割者的设计中:不要认为有两个动作的视图,而是考虑多个视图更改单个数据的状态。
在伪代码中类似于:
// 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);https://stackoverflow.com/questions/8921242
复制相似问题