我有工具栏按钮,我试图通过使用 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);
}
}