The following are a demonstration of how to create a program with JTray
Example1.java
This example demonstrates much of the basic JTray functionality including creating a JTrayIcon and displaying it. To run the example, double click the included runExample1.bat file. Classes and source code are included in the example directory. Below are some excerpts from the source code.
Create a JTrayIcon instance and add click targets
// create an instance of a JTrayIcon which uses image\ani_01.gif
// as the icon to display
jTrayIcon = new JTrayIcon("example\\image\\ani_01.gif",
ImageFileType.JAVA_SUPPORTED);
// specify the tool tip text
jTrayIcon.setToolTipText("JTray Example1");
// set the right click target to the popup menu after
// the jPopupMenu and its components have been completely defined
jTrayIcon.setRightClickTarget(jPopupMenu);
// set the left click target to the slider
jTrayIcon.setLeftClickTarget(slider);
// display this JTrayIcon
jTrayIcon.show();
Example2.java
This example demonstrates some of the more advanced features of JTray such as animated icons and components that don't trigger a popup to disappear. To run the example, double click the included runExample2.bat file. Classes and source code are included in the example directory. Below are some excerpts from the source code.
Specify No Hide Components that are in a click target
// the following addNoHideComponent methods only work
// properly in JRE 1.4 or later
// set the tabbed pane as a no hide component so it can
// be clicked without making it disappear
jTrayIcon.addNoHideComponent(jTabbedPane);
// set the message text area as a no hide component so
// it can be clicked on and used to accept text
jTrayIcon.addNoHideComponent(jTextAreaMessage);
// add the scroll bars of the scroll pane with the
// text area so that they can be used to scroll without
// making the target disappear
jTrayIcon.addNoHideComponent(jScrollPane2.getHorizontalScrollBar());
jTrayIcon.addNoHideComponent(jScrollPane2.getVerticalScrollBar());
Define an animation and set it as the JTrayIcon
// define an animation
IconAnimation iconAnimation = new IconAnimation();
iconAnimation.addIcon(loadImage("example/image/ani_01.gif"));
iconAnimation.addIcon(loadImage("example/image/ani_02.gif"));
iconAnimation.addIcon(loadImage("example/image/ani_03.gif"));
iconAnimation.addIcon(loadImage("example/image/ani_04.gif"));
iconAnimation.addIcon(loadImage("example/image/ani_05.gif"));
iconAnimation.addIcon(loadImage("example/image/ani_06.gif"));
iconAnimation.addIcon(loadImage("example/image/ani_07.gif"));
iconAnimation.addIcon(loadImage("example/image/ani_08.gif"));
jTrayIcon.setAnimation(iconAnimation);