Questions tagged [event-dispatch-thread]

The event dispatch thread, or EDT, is a special background thread which handles events from the Java GUI event queue. Swing and Android have different implementations but they are similar in concept.

This thread must never be stalled with some computationally intensive or otherwise slow task (like networking), as the GUI stops responding to user input until the call returns.

As Java GUI environments are not thread-safe, it is the only thread that can safely touch GUI components (instantiating and updating GUI components, including setting values for text fields and the like). If any other thread must do this, this should be done through a wrapper method that runs on the EDT. In Swing, the wrapper method is SwingUtilities.invokeAndWait or SwingUtilities.invokeLater. In Android, Activity.runOnUiThread serves the same purpose.

775 questions
6
votes
5 answers

Why does my GUI still hang even after using SwingUtilities.invokeLater?

I have this ActionListener that gets called in the EDT. My plot() function is computationally heavy, it can easily take five seconds. It made the GUI hang as expected. I added the SwingUtilities.invokeLater code and it still hangs. Shouldn't the GUI…
smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113
6
votes
1 answer

How to combine Swing and SpringFramework?

Swing requires to run from within Event Dispatch Thread (EDT). How to ensure this in Spring context? In some tutorials, like this one, swing components are just instantiated like normal beans. Is this ok?
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
6
votes
2 answers

Swing application initialization and loading screen approach

I have made quite a lot of various Swing apps and their loading time usually vary between just a few seconds and minutes depending on application UI/data size. Also in some cases application data loading is mixed with UI loading. A few seconds load…
Mikle Garin
  • 10,083
  • 37
  • 59
6
votes
2 answers

javax.swing.Timer vs java.util.Timer inside of a Swing application

is this better to use javax.swing.Timer inside of a swing application instead of using java.util.Timer? for example: Timer timer = new Timer(1000, e -> label.setText(new Date().toString())); timer.setCoalesce(true); timer.setRepeats(true); …
FaNaJ
  • 1,329
  • 1
  • 16
  • 39
6
votes
2 answers

How to check a Swing application for correct use of the EDT (Event DIspatch Thread)

I have found many tutorials and examples on using the EDT correctly, I would however like to hear how one should go the other way around: check a complex application which has a Swing GUI and many functionalities involving long network operations…
dendini
  • 3,842
  • 9
  • 37
  • 74
6
votes
3 answers

Can't override process() method in SwingWorker

I have a SwingWorker class as follows: class RemotePlayersWorker extends SwingWorker { PlayerCanvas parent; RemoteHandler remote; String[][] players; int maximumConnections; public…
BnMcG
  • 668
  • 9
  • 24
5
votes
2 answers

Dynamically removing component from JPanel

Here is runnable piece of code explaining the problem - I can remove s1 and s2 but not s3. This does not seem MigLayout related (I happen to be using it) as I see the same behavior with default layout as well. import…
Amol Katdare
  • 6,740
  • 2
  • 33
  • 36
5
votes
4 answers

Find event dispatch thread violations

We all know we should do all GUI related tasks from the event dispatch thread and that weird bugs can be introduced otherwise - I try to remember this rule but I must admit I've noticed a couple of places recently where I haven't. Is there a way to…
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
5
votes
2 answers

Why does setSelected on JCheckBox lose effect?

Can someone explain to me why I lost the selection (set by setSelected()) for JCheckBox when I put the JOptionPane into the ItemListener? Is this a bug ? It is curious, that if this process is delayed with invokeLater(), setSelected() works…
mKorbel
  • 109,525
  • 20
  • 134
  • 319
5
votes
4 answers

Passing variables to the Event Dispatch Thread

My GUI locks up because I need to update it through the EDT, however, I need to also pass a variable that is being updates with the GUI: while ((message = this.in.readLine()).startsWith("NUMPLAYERS")) { numOfPlayers =…
Logan Serman
  • 29,447
  • 27
  • 102
  • 141
5
votes
4 answers

How to update/paint JProgressBar while Swing is loaded building the GUI

I have a GUI which is quite heavy to build/initialize on the platform on which it runs.. Therefore I want to update progress while it initializes.. I have a small undecorated JDialog containing a JLabel and a JProgressBar which I want to update at…
5
votes
1 answer

SecondaryLoop.enter() not blocking until exit() is called on the EDT

Summary For some reason when I call SecondaryLoop.enter() on the AWT Event Dispatch Thread (EDT), it does not wait for SecondaryLoop.exit() to be called before unblocking. Background Since I think SecondaryLoop is not a very well-known class, I'll…
NateW
  • 908
  • 1
  • 8
  • 28
5
votes
6 answers

How to prompt a confirmation dialog box in the middle of non event dispatching thread

I have the following fun which will be executed by non event dispatching thread. In the middle of thread, I want a A confirmation box pop up. Thread suspend its execution. User makes a choice. Thread will get the choice and continue…
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
5
votes
6 answers

Java: debugging with SwingUtilities.invokeLater()

I use SwingUtilities.invokeLater() frequently. Doing so, however, makes it difficult to debug in certain cases: you can't see a stack trace of the code that called SwingUtilities.invokeLater(), because that code has already ended its execution. Are…
Jason S
  • 184,598
  • 164
  • 608
  • 970
5
votes
2 answers

Why Swing components should be accessed on the Event Dispatch Thread only?

The above statement is mentioned in the SwingWorker javadoc. In an application I have seen a lengthy background task runs in a distinct thread and updates the UI as well without a problem (a reference to a Swing component was accessible). Could it…
yannisf
  • 6,016
  • 9
  • 39
  • 61