A simple window program with System Tray.
Introduction
A simple windows program to show usage of window program and System Tray.
1. Edit a java source file: "X:\demos\src\hello\systray\DemoSystemTray1.java"
package hello.systray; import javax.swing.SwingUtilities; import java.awt.BorderLayout; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JCheckBox; import com.regexlab.j2e.*; public class DemoSystemTray1 extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JCheckBox jCheckBox = null; private SystemTray systemTray = null; private SystemTrayMenu systemTrayMenu = null; /** * This method initializes jCheckBox * * @return javax.swing.JCheckBox */ private JCheckBox getJCheckBox() { if (jCheckBox == null) { jCheckBox = new JCheckBox(); jCheckBox.setText("Show System Tray"); jCheckBox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent e) { if(jCheckBox.isSelected()) { getSystemTray().Show(); } else { getSystemTray().Hide(); } } }); } return jCheckBox; } /** * @param args */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { DemoSystemTray1 thisClass = new DemoSystemTray1(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); } /** * This is the default constructor */ public DemoSystemTray1() { super(); initialize(); // Safely remove systray icon when System.exit() Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { getSystemTray().Hide(); } }); // Callback SystemTray.setSystemTrayCallback(new SystemTrayCallback() { public void OnMouseClick(SystemTray tray, int mouseEvent) { if(mouseEvent == SystemTray.RIGHT_CLICK) { getSystemTrayMenu().Popup(); } } public void OnMenuCommand(int menuid) { if(menuid == 1) { getJCheckBox().setSelected(false); getSystemTray().Hide(); } else if(menuid == 2) { System.exit(0); } } }); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(300, 200); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(getJCheckBox(), BorderLayout.CENTER); } return jContentPane; } /** * Create System Tray * * @return */ private SystemTray getSystemTray() { if(systemTray == null) { systemTray = new SystemTray( 1, // 1 - the first 'icon image', etc "Hello System Tray" // the tips string ); } return systemTray; } private SystemTrayMenu getSystemTrayMenu() { if(systemTrayMenu == null) { systemTrayMenu = new SystemTrayMenu(); systemTrayMenu.Append("Hide", 1); systemTrayMenu.AppendSeparator(); systemTrayMenu.Append("Exit", 2); } return systemTrayMenu; } }
2. Enter "X:\demos" directory and compile it to a class file:
X:\>cd demos X:\demos>javac -cp lib/jar2exe_integrate.jar src/hello/systray/DemoSystemTray1.java
3. Make a jar file:
X:\demos>jar -cvf demosystray1.jar -C src .
4. Use Jar2Exe to generate exe file for Windows only:
X:\demos>j2ewiz demosystray1.jar /m hello.systray.DemoSystemTray1 /type windows /systray
The System Tray supports Windows only.
5. Download the program for reference.
- demosystray1.zip - 98k, download this demo program.
- demos.7z - 5.75M, all demos in one file.
See Also
- See System Tray page for more details about System Tray.