Post a Comment. Everyone who is doing programming in java swing has to come across invokeAndWait and invokeLater provided by SwingUtilites. In this java swing tutorial, we will learn about both invokeLater and invokeAndwait method. In the first part, we will mostly focus on invokeLater and will find answers to questions like What is invokeLater, how to use invokelater in java swing, an example of invokelater in swing, etc while in the second part of this invokeLater tutorial we will learn more about invokeAndWait method in java swing and will learn Why we need InvokeAndWait, how to use InvokeAndWait method in java Swing and differences between invokelater and invokeAndWait.
Finally, we will see a code example of both invokeLater and invokeAndWait in Swing and will be able to decide when to use invokeLater and when to use invokeAndWait while doing Swing programming. We will also see famous Swing interview questions "difference between invokeLater and invokeAndWait" at the end of the article. Before using invokelater or going deep about invokelater lets see why do we need this method in swing utility class?
As we all know java swing is not threadsafe, you can not update swing components like JButton, JLable, JTable or JTree from any thread, they all need to be updated from just one thread and we call it Event Dispatcher thread or EDT in short.
Event Dispatcher thread is used to render graphics for java swing components and also process all events corresponding to a keypress, mouse click or any action. So if you want to update a particular swing component suppose label of a JButton from Yes to No you need to do this in Event Dispatcher thread and for doing this you need InvokeLater.
The invokeLater is a method in java on swing package and belongs to the SwingUtilities class. Invokelater is used by java swing developer to update or perform any task on Event dispatcher thread asynchronously. If you see the signature of the invokeLater method you will find that invokeLater takes a Runnable object and queues it to be processed by EventDispatcher thread. Even if invokeLater is called directly form Event dispatches thread processing of Runnable task still be done only after processing all pending AWT Events.
An important point to note is that in case if the run method of Runnable task throws an exception then AWT Event dispatcher thread will unwind and not the current thread. As we know that Swing is not thread-safe and we can not update the Swing component or GUI from any thread. If you try to update GUI form any thread you will get unexpected results or exceptions, it could be your GUI might not be visible or simply disappeared.
The only method which is thread-safe in the swing is a repaint and revalidate. InvokeAndWait method also belongs to swingUtility class like invokeLater. If you look at the signature of the invokeAndWait method you will see that it takes a Runnable object and run method of that Runnable is executed synchronously on EDT.
In any other kind of program, scheduling the GUI-creation task is usually the last thing the initial thread does, so it doesn't matter whether it uses invokeLater or invokeAndWait. Why does not the initial thread simply create the GUI itself? Because almost all code that creates or interacts with Swing components must run on the event dispatch thread. This restriction is discussed further in the next section.
All rights reserved. Learn more. Asked 5 years ago. Active 5 years ago. Viewed times. Improve this question. Chin Chin Add a comment. Active Oldest Votes. SWT does provide a method of using Swing code but this should be a last resort.
You would get invokeAndWait waiting for the current event to finish, and the current event waiting for invokeAndWait to finish. That's a guaranteed deadlock, and that's why it isn't allowed. Of course Sun could have decided, in the past, to just call runnable. Now they can't anymore because it would no longer do what the Javadoc says.
Boost this thread!
0コメント