Click here to Skip to main content
Click here to Skip to main content

Tagged as

Eclipse Java EventQueue Templates

, 24 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Creating templates in Eclipse to generate Java EventQueue statements

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.

Eclipse Preferences dialog

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}) {
    // ${todo}: Handle exception.
    ${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}) {
    // ${todo}: Handle exception.
    ${exception_variable_name}.printStackTrace();
} catch (InterruptedException ${exception_variable_name}) {
    // ${todo}: Handle exception.
    ${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) {
	    // TODO: Handle exception.
	    exception.printStackTrace();
	}

Example of the statements generated by using the "InvokeAndWait" template:

	try {
	    EventQueue.invokeAndWait(new Runnable() {
		public void run() {
		    
		}
	    });
	} catch (InvocationTargetException exception) {
	    // TODO: Handle exception.
	    exception.printStackTrace();
	} catch (InterruptedException exception) {
	    // TODO: Handle 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Osmund Francis
Software Developer
South Africa South Africa
I'm a graduated electronic/computer engineer. My passions are mathematics and electronics. Although I didn't study software engineering as a main subject, I enjoy it just the same; creating useful pieces of software that can be used by people to increase their productivity is a kind service that invigorates me.
 
I started programming in Pascal, later moving on to C/C++ at university. Java™ became my preferred language in 2005 when I started tinkering with Java™ ME to create applications for mobile devices.
 
I now work with various programming languages, including C#, and even some interpreter languages like SQL.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150123.1 | Last Updated 24 Jul 2014
Article Copyright 2014 by Osmund Francis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid