2

I have two GUI frames in my Java app . I have noticed that both the frames are handled by the same EDT. It makes sense because user is way slower in generating events as compared to the handling of them by the processor. But if I put a sleep call in action performed method EDT sleeps for that specified time. That truly makes sense but the problem is that while EDT was sleeping I could able to generate the events with my other frame though it was not showing animation of the button click but the event is handled by the EDT once it comes out of the sleep. I am not able to understand the flow please help me out.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

4

Suggestions:

  • NEVER call Thread.sleep(...) on the Swing event thread. NEVER.
  • If you need two windows (and make sure that you really do need this), one should probably be a dialog.
  • If you want to "freeze" the events in one window while the second is active, make the second window a modal JDialog or JOptionPane (same thing really).
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
4
  • You should never Sleep on the EDT, nor perform any time consuming actions.
  • The reason why you get all the mouse clicks "spilled" when the EDT wakes up, is that the events are fetched to the Event Queue from outside the EDT, and are handled whenever the EDT is available to handle them.
Elist
  • 5,313
  • 3
  • 35
  • 73
  • 1
    GMTA, you beat me there :) – Elist Jul 21 '13 at 12:32
  • @user2594479: I was probably wrong (also updated my answer). The events are generated by asynchronous interrupts to the OS, where [ISR]{http://en.wikipedia.org/wiki/Interrupt_handler}s are handling inserting them into the queue. The point is that the EDT does not have to be "free" while these asynchronous calls occur. – Elist Jul 21 '13 at 15:30
  • @Elist: But why it was not showing the animation on the button of the second frame while EDT was sleeping. It was like the button was freezed but the event was actually generated. Is the animation of button click related to EDT? – user2594479 Jul 22 '13 at 08:44
  • @user2594479: Of course. The animation occurs when the `InputAction` is generated by the EDT (and sent to the `ActionListener`s) – Elist Jul 22 '13 at 11:26