- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we call the invokeLater() method in Java?
An invokeLater() method is a static method of the SwingUtilities class and it can be used to perform a task asynchronously in the AWT Event dispatcher thread. The SwingUtilities.invokeLater() method works like SwingUtilities.invokeAndWait() except that it puts the request on the event queue and returns immediately. An invokeLater() method does not wait for the block of code inside the Runnable referred by a target to execute.
Syntax
public static void invokeLater(Runnable target)
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class InvokeLaterTest extends Object { private static void print(String msg) { String name = Thread.currentThread().getName(); System.out.println(name + ": " + msg); } public static void main(String[] args) { final JLabel label= new JLabel("Initial text"); JPanel panel = new JPanel(new FlowLayout()); panel.add(label); JFrame f = new JFrame("InvokeLater Test"); f.setContentPane(panel); f.setSize(400, 300); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocationRelativeTo(null); f.setVisible(true); try { print("sleeping for 5 seconds"); Thread.sleep(5000); } catch(InterruptedException ie) { print("interrupted while sleeping"); } print("creating the code block for an event thread"); Runnable setTextRun = new Runnable() { public void run() { try { Thread.sleep(100); print("about to do setText()"); label.setText("New text"); } catch(Exception e) { e.printStackTrace(); } } }; print("about to call invokeLater()"); SwingUtilities.invokeLater(setTextRun); print("back from invokeLater()"); } }
Output
main: sleeping for 5 seconds main: creating the code block for an event thread main: about to call invokeLater() main: back from invokeLater() AWT-EventQueue-0: about to do setText()
- Related Articles
- Can we call Superclass’s static method from subclass in Java?
- Can we call the wait() method without acquiring the lock in Java?
- Can we call a constructor directly from a method in java?
- Can we call run() method directly instead of start() in Java
- When can we call @JsonAnyGetter and @JsonAnySetter annotations in Java?\n
- Can we call methods of the superclass from a static method in java?
- How do we call a Java method recursively?
- How can we implement the paintComponent() method of a JPanel in Java?\n
- Can we call a method on "this" keyword from a constructor in java?
- How can we call garbage collection (GC) explicitly in Java?
- Can we define an abstract class without abstract method in java?\n\n
- Can we overload or override a static method in Java?\n
- Can we declare a main method as private in Java?\n
- Can we call methods using this keyword in java?
- What will happen if we directly call the run() method in Java?

Advertisements