Browse DevX
Sign up for e-mail newsletters from DevX


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.




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

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).

Figure 6: Tray Menu
Next, unzip the "wintray.jar" file, which contains two important java classes:
  1. "WinTray.java" — adds menu items in the tray popup menu; sets icons for the menu items; checks/unchecks or enables/disables menu items; and receives events back in Java
  2. "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.

Add the following line in the import section:

import alexiworld.yahoo.com.*;

Next, add these lines at the end of the main method of MyServer.java:

if (WinTray.isWinTrayAvailable()) { WinTrayListener wtl = new WinTrayListener() { public void actionPerformed(int code) { System.out.println("event: " + code); if (code == 0) System.exit(0); } }; WinTray.addWinTrayListener(wtl); }

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.

2) Add Custom Menu Items in the Tray Menu.
This step creates three separate menu items without icons:

  • enabled
  • disabled
  • checked

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:

WinTray.addWinTrayListener(wtl); ... WinTray.addTrayMenuItem("java action 1", 100, -1); WinTray.addTrayMenuItem(null, 101, -1); WinTray.addTrayMenuItem("java action 2", 102, -1); WinTray.enableTrayMenuItemByID(102, false); WinTray.addTrayMenuItem(null, 103, -1); WinTray.addTrayMenuItem("java action 3", 104, -1); WinTray.checkTrayMenuItemByID(104, true); ....

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.

Figure 7: Event Is Processed
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:


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


Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



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