Browse DevX
Sign up for e-mail newsletters from DevX


Take Charge of Desktop Integration with Java SE 6 : Page 3

With the release of the Java Standard Edition 6.0 Beta, Java developers no longer need to contend with clamp-on solutions or Java Native Interface (JNI) to make their applications interact with desktop products and features. These desktop-integration features now are part of the core.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using JAR's Manifest Attribute
The JAR file in the code download contains the file gomustang-splash.gif, which you will display as a splash screen when the application starts, and the compiled class file GoMustang.class. The JAR's Manifest contains the name of the class containing the main() method and the SplashScreen-Image attribute, which points to the gomustang-splash.gif file.

The following listing is the Ant build.xml JAR task that specifies the main-class and SplashScreen-Image attributes:

... <jar jarfile="${dist}/GoMustang.jar" basedir="${build}"> <manifest> <attribute name="Main-Class" value="GoMustang"/> <attribute name="SplashScreen-Image" value="gomustang- splash.gif"/> </manifest> </jar> ...

To see the splash screen in action, execute build.xml's [run] target by entering ant run at the command prompt. Given the lightweight nature of the application, Java application launcher will open and close the splash screen before the human eye can see it, so you have introduce a pause of three seconds.

Upon running the run target, Ant should write console traces as shown in Figure 1 and then pause for three seconds, during which the user gets to see gomustang-splash.gif as the splash screen.

Click to enlarge

Figure 1. Ant Paused

After three seconds, the Ant script finishes the creation of the application as shown in Figure 2.

Click to enlarge

Figure 2. Ant Script Finishes Creation of Tray Icon

An icon will be created in the system tray as shown in Figure 3.

Click to enlarge

Figure 3. GoMustang Icon

Use a Command Line -splash Parameter
The second approach for showing a splash screen is passing the splash screen file name using the –splash command line parameter as follows:

C:\mustang\build> java -splash:gomustang-splash.gif GoMustang

Notice that for the command to work, you need to use the command line Java from the c:\mustang\build directory where the compiled class file and splash screen GIF files reside. These files are created by tasks in Ant's build.xml file.

Once the application gains access to a thread, you can access the splash screen programmatically using the java.awt.SplashScreen class. This is a singleton class that provides functions to change the image of splash screen, retrieve the size and bounds of the splash screen, get and manipulate the associated graphics object, and then finally close the splash screen.

The GoMustang.java File

Before going any further, you should get acquainted with the GoMustang.java file. The file defines a single Java class, GoMustang, and contains the following two key functions:
  • main
  • createTrayIcon()

As you would suspect, main will be the entry point where one might access the java.awt.SplashScreen Java class to manipulate the splash screen. In the GoMustang application, you have it pause for a few seconds and then close the splash screen by calling splashScreen.close(). However, you have the capability to manipulate the splash screen before closing it.

The createTrayIcon() class is the more exciting function of the two. It does all the interesting work of setting up the application, which is a perfect segue to the next topic: the system tray icons.

Thanks for your registration, follow us on our social networks to keep up-to-date