我有工具栏按钮,我试图通过使用action.setEnabled(false)禁用,但当我这样做时,我得到以下错误:
java.lang.IllegalStateException: This must happen in the event thread!
at org.openide.awt.Actions$Bridge.propertyChange(Actions.java:929)
at org.openide.util.WeakListenerImpl$PropertyChange.propertyChange(WeakListenerImpl.java:197)
at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
at javax.swing.event.SwingPropertyChangeSupport.firePropertyChange(SwingPropertyChangeSupport.java:92)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
at javax.swing.AbstractAction.firePropertyChange(AbstractAction.java:276)
at javax.swing.AbstractAction.setEnabled(AbstractAction.java:236)
调用以下代码后发生:
Action a = new ActionsHelper().findAction("StopDataLogging");
a.setEnabled(false);
但是当我在actionPerform方法中执行时它不会显示错误:
public void actionPerformed(ActionEvent e) {
// TODO implement action body
Action a = new ActionsHelper().findAction("StopDataLogging");
if (a != null){
if (a.isEnabled()){
a.setEnabled(false);
this.setEnabled(true);
}else{
a.setEnabled(true);
this.setEnabled(false);
}
}
}
在事件线程之外启用/禁用操作的正确方法是什么?
更新:我甚至创建了自己的动作事件,我尝试在actionPerformed中使用setEnable,而不是像这样的事件,它仍然显示相同的错误
ActionEvent actionEvent = new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, "DataLoggingCompleted");
a.actionPerformed(actionEvent);
.........
public void actionPerformed(ActionEvent e) {
// TODO implement action body
if(e.getActionCommand().contains("DataLoggingCompleted")){
setEnabled(false);
}
}
我不确定,但也许一个解决方案是使setEnabled方法同步