You need to find an image for the tray icon. I just searched dog on google image search and found one. When you have your image, resize it to 16x16 and save it as a gif or jpeg in your project directory.
Now you need to create a few objects that represent the system tray, the system tray icon, its popup menu, and the items in the popup menu.
Create an Image object to represent the tray image: Image dogImage = Toolkit.getDefaultToolkit().getImage(./dog.jpg) If you use netbeans place your dog.jpg image in your projects root dir. On my windows box this is My Documents/NetBeansProjects/FrameTest.
Now you need to create a SystemTray object: SystemTray sysTray = SystemTray.getSystemTray();
Your next step is to create the popup menu: Popupmenu menu = new PopupMenu();
Next you need to create a MenuItem object which will be added to the popup menu: MenuItem item1 = new MenuItem("Exit");
Now that you have your menu item, you need to add it to the popup menu with PopUpMenu's .add() method: menu.add(item1);
Next, you add an ActionListener to the menu item so that it does something when you click on it.
You now create your Tray icon object: TrayIcon tray = new TrayIcon(dogImage, "App", menu); SystemTray takes 3 arguments, the Image for the tray, a String for the tooltip when you hover your cursor over the tray icon, and the PopUp Menu.
Finally, you take the TrayIcon you created and add it to the SystemTray.
This may sound complicated, but it was pretty easy to do. Here's my working code.
public class FrameTest {
private JFrame frame;
private Image dogImage;
private SystemTray sysTray;
private PopupMenu menu;
private MenuItem item1;
private TrayIcon trayIcon;
//constructor
public FrameTest() {
initComponents();
//basic stuff.
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void initComponents() {
//create jframe
frame = new JFrame("Frame Test");
//check to see if system tray is supported on OS.
if (SystemTray.isSupported()) {
sysTray = SystemTray.getSystemTray();
//create dog image
dogImage = Toolkit.getDefaultToolkit().getImage("./dog.jpg");
//create popupmenu
menu = new PopupMenu();
//create item
item1 = new MenuItem("Exit");
//add item to menu
menu.add(item1);
//add action listener to the item in the popup menu
item1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//create system tray icon.
trayIcon = new TrayIcon(dogImage, "Dog App.", menu);
//add the tray icon to the system tray.
try {
sysTray.add(trayIcon);
}
catch(AWTException e) {
System.out.println(e.getMessage());
}
}
}//end FrameTest constructor
//main
public static void main(String[]args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new FrameTest();
}
});
}//end main()
}//end FrameTest Class
One feature I like in applications that take advantage of the system tray is the ability to minimize an application to the system tray. My favorite Bittorrent app, vuze, allows you to do this. As vuze is written in Java, I started thinking about how easy it would be to implement this, and surprisingly, it is very easy.
Here's how to modify the code in my system tray example (above) to minimize your app to the system tray so that it takes up no space on your task bar:
The first thing you need to do is modify the JFrame's default close operation. Search the constructor for:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
and set it to:
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
This makes it so that instead of exiting when you click 'x' in the title bar, the applications hides itself from view and takes up no space on your task bar. Great. First part done, but now how do we bring it back? It's surprisingly simple. All we do is set the frame to visible.
We need to add another item to the popup menu we created in my first post. We add this to the top of the class, under MenuItem item1:
private MenuItem item2;
Now, in the initComponents() method we add:
item2 = new MenuItem("Show app");
//add actionListener to second menu item
item2.addActionListener(new ActionListerner() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(true);
}
});
//add second item to popup menu
menu.add(item2);
That's it! When you close the app by click x at the top of the app, it should hide and minimize to the system tray. Right clicking on the tray icon should allow you to then "show app" to bring it back.
This post has been edited by farrell2k: 08 September 2011 - 02:42 PM





MultiQuote



|