Access System Tray in AWT

Now, create tray icons on the Windows Taskbar with AWT. The java.awt.SystemTray and java.awt.TrayIcon are the classes that help you. The following example illustrates it.



See how the code goes!

AWT System Tray Window

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class AWTSystemTray extends JFrame
{
JButton jb;
SystemTray tray;
TrayIcon t;

public AWTSystemTray()
{
setTitle("AWT System Tray");
setSize(400,400);
setVisible(true);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);

jb=new JButton("Hide");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
tray=SystemTray.getSystemTray();
t=new TrayIcon(new ImageIcon("C:\\Users\\Computer\\Downloads\\icon11.jpg").getImage());
t.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me)
{
if(me.getButton()==MouseEvent.BUTTON1)
{
setVisible(true);
tray.remove(t);
}
}
});

try
{
PopupMenu m=new PopupMenu();
MenuItem exit=new MenuItem("Exit");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});

m.add(exit);

MenuItem remove=new MenuItem("Hide Icon");
remove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
tray.remove(t);
setVisible(true);
}
});

m.add(remove);

t.setPopupMenu(m);

tray.add(t);

setVisible(false);

}catch(Exception e){}
}
});

add(jb);
}

public static void main(String args[])
{
new AWTSystemTray();
}
}

SystemTray in AWT - Explanation


jb=new JButton("Hide"): Create a new JButton with text Hide

When JButton is clicked..


tray=SystemTray.getSystemTray(): This is a method of the java.awt.SystemTray class which returns a java.awt.SystemTray object with which we can add tray icons to the system tray. You'll have to note that not all Operating systems support system tray.

t=new TrayIcon(new ImageIcon("C:\\Users\\Computer\\Downloads\\icon11.jpg").getImage()): This constructor in java.awt.TrayIcon class takes java.awt.Image as parameter which points out the image to be displayed as icon in the system tray. For this, i've used javax.swing.ImageIcon class which has a constructor that takes java.lang.String as parameter which represents the path of the image file. The javax.swing.ImageIcon consists of a method called getImage() which returns a java.awt.Image object pointing to the image specified (here the image is icon11.jpg and the path is specified above).

What if user clicks on Tray icon?


me.getButton(): This method returns the button which the user has clicked ex. left click or right click... MouseEvent.BUTTON1 represents left click.

setVisible(true): Make the frame visible. This is done when user left-clicks on the tray icon.

tray.remove(t): Remove tray icon. When frame is visible, i don't need a tray icon. That's why i've removed it.

p=new PopupMenu(): Create a popup menu, the menu that appears when user right-clicks on any component to which it is added.

System.exit(0): Exit the program when the user clicks on menu item exit

tray.remove(t): Remove icon from the tray, when the user clicks on menu item remove (which has text Hide Icon) on it.

setVisible(true): When icon is no more, the frame has to visible, else the user will suffer.

t.setPopupMenu(m): Set the popup menu named m which contains menu items exit,remove to the TrayIcon. (Right click on the tray icon to see this menu)

tray.add(t): Add the tray icon t to tray.

setVisible(false): When tray icon is visible, i don't need the frame to be visible. Make either one visible.

Also see..
  1. Creating JButtons in Swing
External links that may help you..

No comments: