Introduction
Programmers who implement and test Swing applications and applets need to adhere to guidelines regarding execution. This tip shows detailed instructions of how to create a template in Eclipse to easily generate the appropriate code.
Instructions
1. Managing Java™ Templates in Eclipse
To manage Java™ templates in Eclipse, use the menu Windows / Preferences menu item.
In the preferences dialog, select Java/Editor/Templates from the tree list.

2. Create a New Java™ Template in Eclipse
From the Eclipse Preferences dialog (Java/Editor/Templates), clicking the "New" button opens up a dialog to create a new template.
Below are images showing how a template was defined for the Java™ template "InvokeLater
" and "InvokeAndWait
":


Code snippet for InvokeLater
template:
try {${:import('java.awt.EventQueue')}
EventQueue.invokeLater(new Runnable() {
public void run() {
${line_selection}${cursor}
}
});
} catch (Exception ${exception_variable_name}) {
${exception_variable_name}.printStackTrace();
}
Code snippet for InvokeAndWait
template:
try {${:import('java.awt.EventQueue', 'java.lang.reflect.InvocationTargetException')}
EventQueue.invokeAndWait(new Runnable() {
public void run() {
${line_selection}${cursor}
}
});
} catch (InvocationTargetException ${exception_variable_name}) {
${exception_variable_name}.printStackTrace();
} catch (InterruptedException ${exception_variable_name}) {
${exception_variable_name}.printStackTrace();
}
3. Import Java™ Templates into Eclipse
Alternately, one can import a template. Templates can be exported to XML files, after which they can later be imported into any Eclipse installation.
From the Eclipse Preferences dialog (Java/Editor/Templates), clicking the "Import" button opens up a dialog to import a template, and clicking the "Export" button will open a dialog to save a selected template.
Using the Templates
To use the templates, simply invoke the content assistant while editing a Java™ source file. Selecting the "InvokeLater
" template will produce the EventQueue
statements for use in a Java application. Selecting the "InvokeAndWait
" template will produce the EventQueue
statements for use in a Java™ application and/or applet.
The templates include instructions to import the needed classes. If there are any conflicts, the import will not add any import statements, and you will need to resolve the import conflicts yourself.
Example of the statements generated by using the "InvokeLater
" template:
try {
EventQueue.invokeLater(new Runnable() {
public void run() {
}
});
} catch (Exception exception) {
exception.printStackTrace();
}
Example of the statements generated by using the "InvokeAndWait
" template:
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
}
});
} catch (InvocationTargetException exception) {
exception.printStackTrace();
} catch (InterruptedException exception) {
exception.printStackTrace();
}
Point of Interest
Experienced programmers may be more familiar with the SwingUtilities invokeLater()
and invokeAndWait()
methods. These methods now map to the EventQueue
methods (since Java™ v1.2). This allows programmers who wish to use other GUI toolkits to do so safely, following the same best practices that need to be adhered to when using the Java Swing GUI toolkit.
Java™ programmers may want to familiarize themselves with how to embed JavaFX components in a Java application, whether in conjunction with Java AWT, or the Java Swing GUI toolkit.
History
- 2014-07-22 Initial submission.
- 2014-07-23 Typgraphical update.
- 2014-08-11 Updated the templates with import instructions.