Camilo Guevara
Camilo Guevara

Reputation: 165

Why I cant disable a toolbar button (action) using setEnabled(false)

I have toolbar button, that I am trying to disable by using action.setEnabled(false), but when I do it I am getting the following error:

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)

this happens after calling this code:

                Action a = new ActionsHelper().findAction("StopDataLogging");
                a.setEnabled(false); 

But when I do it inside the actionPerform method it doesn't show the error:

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);
            }
        } 

}

What is the proper way to enable / disable an action outside an event thread?

UPDATE:

I even create my own action event, and I try to use setEnable within actionPerformed and not event like that works, it stills shows the same error

                    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);
    }
}

Upvotes: 0

Views: 669

Answers (1)

Lucurious
Lucurious

Reputation: 174

I'm not sure, but maybe a solution is to make the setEnabled method synchronized

Upvotes: 0

Related Questions