Summary: This article will give you a concrete example of the outstanding features that Java SE 6 offers in desktop development.
first, the introduction
With the Java SE 6 beta release, Java developers no longer need to implement Java Native Interface (JNI) to incorporate the features of their desktop products into their applications. These desktop integration features have now become an integral part of the kernel.
The latest Java Standard version 6.0 (code name Mustang) will feature a series of improvements-developers can easily use Java to deal with security, JMX, operating system files, internationalization and desktop development, and a range of issues. Sun has shown a very positive and cooperative attitude in developing this version. They listened carefully to the developer's advice through JSR 270 and released all the Java SE Source and binary code early in February 2006. It seems that sun is trying to build a new desktop development tool.
Compared to previous Java versions, Mustang's desktop integration capabilities are particularly popular with Java developers. These new features add a lot of interesting features to the development of Java desktop applications. Previously, developers had to worry about creating Java Native Interface (JNI) such as Jtray and Systray when developing Java such as Internet Explorer, System tray, and mail client. Although this "patch" scenario is achievable, most Java developers are looking forward to these features as part of the core Java platform.
This article will analyze the process of creating a sample application Gomustang in detail. From there, I'll show you how to use Java SE 6 to display a splash screen when an application starts, how to create a system tray, and how to start a native desktop internet browser from within Java.
Note that for debugging the sample program in this article, you need to install:
Java Standard Edition 6 BetaAnd
Apache Ant 1.6.5。
second, development environment
(i) Prepare Java SE 6 Beta
First, you will download the Java SE 6 Beta from Sun's Java site. Since Sun has determined that Java SE 6 will be officially released in the fall of 2006, some of the features of the JSR 270 expert group will be appropriately changed, so it is time to try Mustang in your desktop development.
It is estimated that the features described in this article will not change much, so you can rest assured of using the latest download version of JDK,JRE.
First, make sure your desktop platform is supported. I developed and tested the sample application for this article on Windows XP with no problem.
Second, make sure that the environment variables in the Windows System properties of your computer point to the Java SE 6.0 JDK. I set the JAVA_HOME environment variable to Java_home=c:\program files\java\jdk1.6.0.
Once the installation is complete, you should immediately check the version of the Java JRE. If all goes well, you should display a message similar to the following:
C:\java\mustang> java-version
Java Version "1.6.0-beta"
Java (TM) 2 Runtime environment, Standard Edition (build 1.6.0-beta-b59g)
Java HotSpot (TM) Client VM (build 1.6.0-beta-b59g,mixed mode,sharing)
(ii) Preparing Ant
In this article, you will use Apache ant to build the sample application. Therefore, if you do not have this tool, you can download it from the Apache website and install it on your computer.
You can then use the Ant property to check the ant Java version. The following example shows an incorrect version of Java:
${ant.java.version}
This is because ant's built-in Ant.java.version system properties are designed to have only 1.1,1.2,1.3,1.4 and 1.5 values. It is estimated that the ant development team may soon update this attribute to include 1.6, but be especially careful when you want to do this yourself.
For the sake of prudence, you can use the-debug parameter to run Ant. If the following is displayed, Ant is actually using a 1.6 version of the JRE:
>>
Detected Java version:1.5 In:c:\program Files\java\jdk1.6.0\jre
<<
(iii) Installation of your development environment
Next, install your development environment. To do this, the simplest way is to download the corresponding source code and decompression. If you extract the zip file to your C:\ path, you will see files and directories in the following structure:
c:\mustang\
|_build.xml
+_src
Later, you will use the ant build file Build.xml to replicate, compile, build, package, and run the Gomustang sample application. This file contains the following key ant targets:
[Init]
[Clean]
[Compile]
[Dist]
[Run]
Here, the [init] target establishes the folder structure and copies the files to the build folder, [compile] implements the compilation, and [Dist] creates a jar by using a manifest file (discussed in detail later). The [run] goal is to start your application, and [clean] will help you first delete all the files and directories created by your ant task and start the following tasks from a completely clean environment.
The SRC directory contains 3 files: a Gomustang.java (source file) and two GIF files (Gomustang-splash.gif used as splash screens, gomustang.gif as icons in the system tray).
Now, you've made the groundwork. Below, we begin to create the splash screen of your application.
Third, splash screen
For desktop based applications, the splash screen has always played a key role in the application startup process. A striking splash screen allows users to eagerly look forward to the application's launch-by displaying marketing ads, specific legal texts, professional images, and so on. It also gives the application a certain period of time to load the required library files.
In Java application development, implementing the splash screen has always been a challenging programming process, as the JVM startup and all required JRE and application libraries were not able to gain control before they were loaded. When a Java application gets the chance to control the thread, it's too late to display a splash screen.
Java SE 6 Even allows an application to display a splash screen before the JVM is started. This feature is built into the Java Application Launcher-it is responsible for displaying an image in an unmodified window. It allows the use of gif,png or JPEG images, can be transparent or translucent, and can have animation effects. Also, the Java SE 6 release has a SplashScreen class-it allows the program to manipulate the splash screen once the application controls the thread.
You can start a splash screen in one of the following two ways:
1. Specify a specific splashscreen-image attribute in the manifest file of a jar;
2. Use a command line-splash parameter.
(i) Using the manifest property of the jar
The jar file in the source code of this article contains a gomustang-splash.gif file (this file displays a splash screen when the application starts) and compiles the class file Gomustang.class. The manifest of this jar file contains the name and Splashscreen-image attribute of the class that defines the main () method (it points to the Gomustang-splash.gif file).
The following list shows the contents of the Ant Build.xml Jar task-which specifies the Main-class and Splashscreen-image properties:
...
<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 real operation of the splash screen, you can perform Build.xml's [run] target by typing ant run at the command prompt. Because the program is small, the Java application completes opening and closing operations before the human eye can see it. To do this, you can use the 3 seconds to pause coding tips.
Once running the run target, ant should output the console trace as shown in Figure 1, then pause for 3 seconds, where the user will see the gomustang-splash.gif picture on the splash screen.
Figure 1. Ant paused
After 3 seconds, the ant script completes the application creation, as shown in Figure 2.
Figure 2. Ant script completes system tray creation
An icon will be created in the system tray, as shown in Figure 3.
Figure 3. Gomustang icon
(ii) using command line-splash parameters
The second way to display a splash screen is to use the-splash command-line argument to pass the splash screen file name, as shown below
C:\mustang\build> Java-splash:gomustang-splash.gif Gomustang
Note that in order for this command to work correctly, you need to use the Java command-line method from the C:\mustang\build directory (the location where the compiled class file and splash screen gif files are located). These files are created by the tasks in the Build.xml file in Ant.
Once an application obtains access to a thread, you can use the Java.awt.SplashScreen class to programmatically access the splash screen. This is a singleton class that provides specialized functions to change the splash screen image, retrieve the size and bounds of the splash screen, manipulate the graphics object, and finally close the splash screen.
Iv. Gomustang.java Documents
Before continuing, you should familiarize yourself with the Gomustang.java file. This file defines a single Java class Gomustang and contains the following two key functions:
· Main
· Createtrayicon ()
You may have guessed that main is the main entry point-you can access the Java.awt.SplashScreen Java class to manipulate the splash screen. In the Gomustang application, you can pause it for a few seconds and then close the splash screen by calling Splashscreen.close (). However, you can manipulate the splash screen before you close it.
The Createtrayicon () class is more exciting in these two functions. It is responsible for implementing all the interesting work to install the application. This involves another topic that we will discuss: System tray icon.
Five, System tray icon
The system tray is located in the task status area of Windows or in the notification area of GNOME. It is a small area in the corner of the desktop UI of an operating system that provides visual and direct access to the currently running application. The Gomustang application on the Windows platform provided by this article enables you to generate a pop-up menu when you right-click the system tray icon (see Figure 4).
Figure 4. Gomustang pop-up menu on the system tray
The Gomustang application Systemtray has a pop-up menu. Menus have two menu items: "Exit" and "Launch Browser". Exit is responsible for exiting the application, and "Launch Browser" is responsible for opening the default Internet browser and pointing it to http://www.devx.com.
In the past, Java applications that created flexible desktop system trays that controlled the operating system needed support from libraries that were not part of the Java core. To this end, Jdic (Java Desktop Integration component) was generated, and Sun decided to release some of the Jdic components as part of the Java SE 6. The related classes Java.awt.SystemTray and Java.awt.Desktop describe these key components. Where Java.awt.SystemTray describes the operating system's task status or notification area.
The icons displayed in the system tray are described by Java.awt.TrayIcon. TrayIcon not only describes the image but also describes the actual object that receives the event, and is responsible for displaying the prompts, adding pop-up menu items, displaying messages, and so on. You can set up message and prompt information on TrayIcon by DisplayMessage (string) and SetToolTip (string) functions. TrayIcon can also forward actionevents to ActionListener registered with the addActionListener (ActionListener) function of TrayIcon. Pop-up menus are described by Java.awt.PopupMenu, and you can add them to TrayIcon via the Addpopupmenu (popupmenu) function.
Next, let's dissect the Createtrayicon () function of the Gomustang Java class. As the detailed comments in Listing 1 (see the attached source code) show: First, you call the systemtray.issupported () function to check whether the operating system supports the minimum systemtray function; if supported, This function should return true.
Once the program knows that the operating system supports the tray function, it creates exit and launch browser menu items and adds them to the pop-up menu. The menu pops up when the user right-clicks the system tray icon. The Actionlisteners function is added to the exit and launch browser menu items. When the user selects these menu items, actionperformed (actionevent) gives the program an opportunity to perform some action. This exits the application for the Exit menu item by executing the system.exit (0) command, and for the launch browser menu item, the default browser is started by pointing to http://www.devx.com. Note that starting the browser requires the use of a new Java.awt.Desktop object (discussed later).
The application then creates a pop-up menu object and adds the exit and launch browser menu items to the pop-up menu. The next step is to create a pallet icon with a horse image. You can use the GetClass (). getResourceAsStream (file name) function to access the Gomustang.gif file in this download source code and load it into an image object. The tray icon object can then be created with the pop-up menu, and the action and the mouse listener are also associated with the tray icon-for this example, only a single message is displayed. The tray icon is finally added to the system tray. The system tray is a singleton object that can be retrieved by the Getsystemtray () function of the Systemtray object. This is the complete process for creating a tray icon on the system tray.
Now, let's take a closer look at how the "Launch Browser" menu item starts the browser. You can analyze the function actionperformed (actionevent) corresponding to the "Launch Browser" menu item to see how to use the Desktop object. Refer to the following code (part of Listing 2):
...
MenuItem Launchbrowseritem = new MenuItem ("Launch Browser");
ActionListener Launchlistener = new ActionListener () {
public void actionperformed (ActionEvent e) {
if (desktop.isdesktopsupported ()) {
Actionmessage = "launched Browser";
try{
Trayicon.displaymessage ("gomustang!",
"Launching Browser ...", TrayIcon.MessageType.INFO);
Desktop Desktop = Desktop.getdesktop ();
Desktop.browse (New URI ("http://dev.yesky.com"));
Trayicon.settooltip ("gomustang!");
}
catch (Exception exp) {...}
}
}
};
Launchbrowseritem.addactionlistener (Launchlistener);
...
The key object that is most interesting to us in Listing 2 (see the attached source code) is the Java.awt.Desktop class. This desktop class, as we mentioned earlier, comes from Jdic. The desktop class is responsible for locating and running operating system-specific desktop applications. The default Internet Browser application mapping is Mozilla Firefox; therefore, after the message "Launching Browser ..." is displayed, the Desktop.browse (URI) function opens a window that points to http://dev.yesky.com.
Vi. Summary
The real interesting thing about Java's development in desktop control is just the beginning. We sincerely look forward to adding more desktop control features to future releases of Java.