-1

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.

AyCe
  • 727
  • 2
  • 11
  • 30

2 Answers2

1

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • Okay, so you would say that `setEnabled(false)` guarantees that there will be no invocation of any ActionListener after it has been called, not even 1 event after it? Well, I'd say that too, but because Java has disappointed me with some of these _contracts_ already, I'm not so sure about them anymore. – AyCe Feb 28 '14 at 06:21
  • @AyCe You can be sure of it, unless you have messed up the code ! – ItachiUchiha Feb 28 '14 at 06:22
0

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

}
}
CodeFreak
  • 31
  • 5
  • Umm... you might not have understood my question. :| I know how to set a button in an enabled/disabled state, but I was wondering if a _disabled_ button can still fire ActionEvents in a very small time after the operation. – AyCe Feb 28 '14 at 06:22
  • @AyCe No it won't. Based on my experience. – CodeFreak Feb 28 '14 at 06:23