AyCe
AyCe

Reputation: 766

JButton: actionPerformed while invisible/disabled?

Is it possible that, for example, a JButton calls actionPerformed() of an ActionListener attached to it while the button itself is hidden or has been disabled?

Like, the user clicks the button, an event gets added to the Event Queue, and in a previous event the button gets disabled.

I'd think that this would not lead to an actionPerformed(), because the user merely submitted a click- or press-event that checks all this stuff in the current JFrame.

But does anybody know if there is any case where such an unwanted situation happens? Of course, always provided that you don't do anything with Swing objects outside of the EDT.

Edit for anyone looking at this: Yes, it can indeed happen under certain circumstances. I should post an example as an answer at some point.

Upvotes: -1

Views: 521

Answers (2)

ItachiUchiha
ItachiUchiha

Reputation: 36792

From the JavaDocs

public void setEnabled(boolean enabled)

Sets whether or not this component is enabled. A component that is enabled may respond to user input, while a component that is not enabled cannot respond to user input. Some components may alter their visual representation when they are disabled in order to provide feedback to the user that they cannot take input.

For more info

http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#setEnabled%28boolean%29

INFO : JButton extends AbstractButton which extends JComponent

Upvotes: 1

CodeFreak
CodeFreak

Reputation: 31

Just use a conditional statement and tell the app to enable jButton when the conditions are met.

For example:

private buttonNameActionPerformed(java.awt.evt evt){

   if(condition.equals(something)){
   jButton.setEnabled(true);

}
else{
jButton.setEnabled(false);

}
}

Upvotes: 0

Related Questions