Questions tagged [invokelater]

A method provided by the SwingUtilities class in Java that executes asynchronously code on the AWT event dispatching thread in order to correctly manipulate UI elements.

SwingUtilities.invokeLater is a method provided by the Swing Library in Java in order to execute code on the AWT dispatching thread in an asynchronous fashion. As in the majority of graphical toolkits, an arbitrary thread is not allowed to update directly the UI but should asks the UI thread to execute it on its behalf.

Similar constructs exists in other UI toolkits (for example, in .NET there are Invoke/InvokeRequired methods in Windows Forms and the Dispatcher object in WPF).

This tag should be used to emphasize that invokeLater is used in the code, thus ruling out concurrency issues on the UI.

Related tags:

Additional resources:

  • The Java Tutorial has a chapter on invokeLater and invokeAndWait.
120 questions
3
votes
3 answers

If EDT is a separate thread, why does invokeLater wait for the main thread to finish in this example?

So if Event Dispatch Thread is a separate thread from the main thread, that makes me think the next code would output Before Runnable true After But when i run it, it's as if the EDT waits for the main thread to finish before running the chunk of…
3
votes
2 answers

SwingUtilities InvokeLater- what is considered bad practice?

I've got a question about what would be the correct practice to use the invokeLater method of SwingUtilities. So to begin, I'd like to confirm that I am understanding it correctly. From what I understand, changes to the GUI must be done on the EDT,…
DBS
  • 63
  • 3
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
2 answers

Query on creating separate thread in java?

Below is the compiled program replica of actual problem code, import javax.swing.JOptionPane; import javax.swing.SwingUtilities; public class Dummy { public static boolean getUserCheck(int size, boolean Check) { if (Check) { …
overexchange
  • 15,768
  • 30
  • 152
  • 347
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
3
votes
2 answers

About the EDT (Java)

I have read a number of articles on the internet about when something should run in the EDT, and when it shouldn't. But I'm still not sure I understand, so I'd like to ask a few question about this: What pieces of code are going to run by default…
user3150201
  • 1,901
  • 5
  • 26
  • 29
3
votes
4 answers

Why is it important to use invokeLater?

I recently found an example code: public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } The createAndShowGUI() method…
Buras
  • 3,069
  • 28
  • 79
  • 126
3
votes
2 answers

Should Swing GUI application be controlled from Event Dispatcher or main thread?

I've read a few books about Java. In all of them there was at least one chapter teaching GUI programming. In all of them, creating a simple form application was following this logic: MyFrame.java public class MyFrame extends JFrame { JButton…
3
votes
2 answers

Java SwingUtilities.invokeLater update TextArea

I have the following classes: MainServer TCPServer UDPServer I create new instances of TCPServer and UDPServer classes (start) from MainServer class, where my GUI is initialized. In this GUI I have a textArea, which either TCP or UDP classes…
gogasca
  • 9,283
  • 6
  • 80
  • 125
2
votes
1 answer

To invokeLater() or Not

I was writing an example to showcase the usage of SwingUtilities.invokeLater(). I came to realize this simple example (code included) I have written didn't warranty the use of invokeLater(). I do encounter there were times I needed to use…
thlim
  • 2,908
  • 3
  • 34
  • 57
2
votes
5 answers

Dynamic JTree and SwingUtilities.invokeLater() does nothing

To create my dynamic JTree, I was reading the tutorial about Dynamic JTrees on this website in chapter "4.2 OutlineNode.java" Now I have implemented it and recognize that loading the data in the GUI thread takes long and is ugly as well. Therefore I…
Atmocreations
  • 9,923
  • 15
  • 67
  • 102
2
votes
4 answers

How does a Java thread synchronize with invokeLater()?

I have a non-GUI thread that starts a JFrame using java.awt.EventQueue.invokeLater(new Runnable() { public void run() { cardReadPunchGUI = new IBM1622GUI(); // instantiate cardReadPunchGUI.setVisible(true); } }); Part of…
Chap
  • 3,649
  • 2
  • 46
  • 84
2
votes
1 answer

Using SwingUtilities.invokeLater() in main method

I recently saw a MVC java application in which the main method was written as: public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { …
2
votes
3 answers

Java SwingUtilities.invokeLater

.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ try{ ta.append("Searching Initiated at: "+datetime()+"\n"); …
user585522
  • 165
  • 1
  • 5
  • 15
2
votes
3 answers

Use invokeLater to show dialogs one by one

In 2 different action listeners, a dialog will be shown when some conditions are met. If both of the action listeners need to show dialog, 2 dialogs will be shown at the same time. But I want them to be shown one by one. Simplified code: …
Leo Jay
  • 481
  • 1
  • 4
  • 6