Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top