Build "Win-tuitive" Java Applications : Page 3

You can make your cross-platform Java application more intuitive for Windows users. Learn how enabling access to the tray bar and programming your application to act as a Windows Service provides this functionality.

by Alexi Jordanov

Jul 28, 2004

Page 3 of 4

WEBINAR:

On-Demand

Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Exercise 2: Exploiting the Windows Tray Bar
Don't be afraid—using the tray bar is easy! Start "J2TrayExe" and follow the same steps you did in the previous example. The only difference is you set the classpath to "-classpath .;wintray.jar" instead of "-classpath . server.MyServer". Press the "Generate" button and check the "c:\J2ExeDemo\bin" directory. You will find two files there: "MyServer.exe" (tray variant now) and "wintray.jar". Move the "wintray.jar" into "c:\J2ExeDemo\lib" since the classpath expects it there.

Now you can start "MyServer.exe". If your application is a Windows application, try to close it. If it's a console application, near the minimize, maximize, and close buttons you'll see an additional button that minimizes the application right into the tray bar. In either case, you will see your application in the tray bar. You can maximize it by double clicking it, and you can show the tray menu by right clicking on it (see Figure 6).

"WinTrayListener.java"— receives events back in Java where you can process them

Now just take the following steps to employ the Windows tray bar in your Java applications:

1) Add Listener to Handle the Close Operation.
Minimize the .exe into the tray bar, right click on the tray icon, and then exit. The icon will disappear and then reappear some minutes later because, in order to perform a soft shutdown, the Java code does not process the close operation. This way, your code can close some connections, save important data, and then quit.

Kill the MyServer process from Task Manager and start a new one. Try again to close it from the Tray menu. Now the icon disappears permanently. If you check in Task Manager, you'll see that this process no longer exists.

You can provide specific icons for these items later using the "addTrayMenuIcon" method of the WinTray class. Note that checked menu items cannot have icons because the checkmark acts as an icon for them.

Add the following lines immediately after registering the listener, compile the class, and then restart the application:

Next, minimize the application, right click it, and select any new items. Then restore the application from the tray bar and show its console (if it was hidden; see Figure 7). You should see the action code dump.

3) Start Your Application Minimized
Since most servers work in the background, the tray bar is the ideal place for them. Therefore, you should minimize your application in the tray bar at startup (or at any time, really) using the WinTray class:

WinTray.minimize();

Alternately, you can restore it from the tray bar using the same class: