I don't know how to use SwingUtilities.invokeLater
precisely, but in my application, the panels run in different Runnables, and swingUtilities.invokeLater
accepts Runnables in the argument, but if I put a thread inside the thread of swingUtilities.invokeLater, it will work inside the "SwingUtilities thread-safe AWT/Swing concept"? and can I put more then 1 thread? because I have panels/threads to run inside 1 JFrame.
Asked
Active
Viewed 182 times
0

Forsaiken
- 324
- 3
- 12
-
Not sure what you mean that your panels run in different threads. it makes no sense. There is a single event dispatcher thread which handles all events and graphic responses. The 'invokeLater' adds events to this thread, i.e. after you done a long task in an arbitrary thread, you signal a gui component to update by invoking correct (short) update commands with invokeLater. They will be added to the end of the event queue. – Serge Apr 23 '18 at 21:35
-
3You're confused as to what `invokeLater` does. It does **not** take a Thread as argument but rather a Runnable, and it queues it on to the Swing event thread to be run on this single event thread. If you want to do background threading, use a SwingWorker, and create multiple workers if need be. Do read [Lesson: Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) to gain a better understanding of this. – Hovercraft Full Of Eels Apr 23 '18 at 21:40
-
Also your question is lacking in essential details and code, and it's hard to give you a specific answer without your improving it. Please have a look at [ask] for more on how to ask a decent question. – Hovercraft Full Of Eels Apr 23 '18 at 21:41
-
Also please have a look at [these similar questions](https://www.google.com/search?&q=site%3Astackoverflow.com+java+swing+multithreading) and their answers. – Hovercraft Full Of Eels Apr 23 '18 at 21:54
-
I will do a example code of this question and a problem with CountDownLatch that I have in the invokeLater that "block the interface" – Forsaiken Apr 23 '18 at 22:01
-
1You really want to take a look at [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) - Swing is single thread AND not thread safe. You should NEVER modify the UI (directly or indirectly) from outside the context of the Event Dispatching Thread – MadProgrammer Apr 23 '18 at 22:02
-
1*"CountDownLatch that I have in the invokeLater that "block the interface""* - If you're running `CountDownLatch` within the context of the EDT - then yes, I imagine it would block the UI – MadProgrammer Apr 23 '18 at 22:02
-
But how can I use CountDownLatch if my Frame is inside a Runnable in invokeLater, like here: https://i.imgur.com/RPFv60d.png I need to use CountDown to freeze the runnable and make the panel work without remove the panel like the example. – Forsaiken Apr 23 '18 at 23:11
1 Answers
0
In Swing only 1 thread (the event dispatch thread) may do updates to the user interface (all Swing UI components of the complete application). Do all the updates on user interface with a runnable that is passed into that invokeLater (or invokeAndWait) method.
Don't do calculations / other tasks in this runnable, because that can slowdown/block your user interface.
There is no maximum number of runnables you may pass into that SwingUtilities methods. All of them are executed by the event dispatch thread.

Jasper Huzen
- 1,513
- 12
- 26