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
3
votes
2 answers

Repainting in a multithreaded environment

i am working on an applet with around ten different datasources(e.g. statistics/error-log/...). Each datasource is updated by a single network connection and reports updates via the observer mechanism. The applet has different views which display…
tfk
  • 402
  • 3
  • 9
3
votes
1 answer

Why is my thread not working properly in Swing?

I am printing simple value to append JTextArea using simple for loop, and when I run it, it's properly Run if I print value in console output... But if I append JTextArea and print value in the text area, they are appended all after whole program…
Bhola
  • 392
  • 1
  • 15
3
votes
7 answers

How to retrieve a value that must be computed on another thread

There are many cases where thread A requires a value that must be computed on thread B. (Most commonly, B == EDT.) Consider this example: String host; SwingUtilities.invokeAndWait(new Runnable() { public void run() { host =…
Matt McHenry
  • 20,009
  • 8
  • 65
  • 64
3
votes
1 answer

Swing: Running code immediately vs invokeLater

I'm currently working with a thread control class someone else wrote. It is used for a java swing application. I have two methods, but I am confused as to why I am getting different behaviors from both. From what I know and read about the event…
3
votes
1 answer

What is the correct way to perform long-running operation outside the EDT?

In a desktop Java 1.5 application (it has to run on a lot of MacOS X machines that will nerver see a 1.6 VM due to Apple politics) what is a correct way to perform a lengthy computation outside the EDT? Say, for example, when the user clicks on a…
NoozNooz42
  • 4,238
  • 6
  • 33
  • 53
3
votes
2 answers

Java Swing EDT : How to know which threads are waiting the execution of an EventDisplay via SwingUtilities.invokeAndWait?

I have a pretty complex problem. In my current project, I have a GUI written in Java and a computing engine written in C++. These are displays in Java which access to data in C++, and I have some issues with concurrency. There are a long story in…
3
votes
3 answers

When and Where to call EventQueue.invokeLater() method

I'm totally fresh about threading and GUIs, therefore I couldn't figure out exactly where to call this EventQueue.invokeLater() method. Am I supposed to call it in every event listeners and something else? What are those "things" to call this…
3
votes
1 answer

paintComponent() gets called only when Minimizing-Maximizing Screen

I am trying to develop a game in which I need to draw a grid. For that I am using the paintComponent(Graphics g) method which is being called by repaint() method. The problem is that the repaint method is inside the infinite While loop and it never…
Manpreet
  • 570
  • 5
  • 20
3
votes
1 answer

When is the Swing UI thread created?

When, in the process of running a Swing program, is the UI thread (event-dispatch thread, EDT) first spawned? Presumably any given JVM could do whatever it wants (for example, always spawning the EDT at start-up, whether or not it's ever used), but…
cal
  • 33
  • 4
3
votes
1 answer

When am I supposed to use javax.swing.SwingUtilities.invokeLater()?

I have a GUI which uses a JProgressBar. The GUI has JBUTTONs that invoke following code: public void updateProgressBar(int x) { System.out.println(javax.swing.SwingUtilities.isEventDispatchThread()); healthBar.setValue(x); } but I have also…
user2651804
  • 1,464
  • 4
  • 22
  • 45
3
votes
4 answers

Handling the Event Dispatch Thread

I have a question about the 'Event Dispatch Thread'. I have a Main class that is also a JFrame. It initialises the rest of the components in the code, some of them do not involve Swing and some of them do. Is it enough to simply initialise the Main…
bcoughlan
  • 25,987
  • 18
  • 90
  • 141
3
votes
4 answers

Returning values from Swing using invokeAndWait

I've been using the following approach to create components and return values from Swing to/from outside the EDT. For instance, the following method could be an extension to JFrame, to create a JPanel and add it to the parent JFrame: public JPanel…
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
3
votes
1 answer

Should non-Swing code be executed on non-EDT threads?

I know the Swing single-thread rule (from Java Concurrency in Practice): Swing components and models should be created, modified, and queried only from the event-dispatching thread. Is the converse also true? I maintain some transaction…
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
3
votes
1 answer

On Event Dispatch Thread---want to get off of it

Suppose that a method I own is sometimes called on the Event Dispatch Thread and is sometimes not. Now suppose some of the code in that method I want to have called on a thread other than the Event Dispatch Thread. Is there a way to run some code…
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
3
votes
1 answer

Java Swing Thread Safety and EDT

I understand that JFrames should be safely created on the Event Dispatch Thread (EDT) using invokeLater, and I'm attempting to make two of them within my main method. Code: public void run() { // Handle Menu When Open …
fanatic
  • 143
  • 2
  • 9