我有工具栏按钮,我试图通过使用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);
}
}
答案 0 :(得分:0)
我不确定,但也许解决方法是使setEnabled方法同步