Java.task tray(in Japanese history)system tray (e.g. in Microsoft Windows)) icons.Indication/Flashing
Java.task tray(in Japanese history)system tray (e.g. in Microsoft Windows)This section introduces utility classes that display and blink the icons of the
In the utility class, the new feature added in Java SE 6 Mustang is SystemTray , ,TrayIcon class. A sample application is also included to illustrate its use.
System tray (task tray) function
The system tray (task tray) is a small icon that lines the lower right corner of the computer screen. Java SE 6 adds the following classes to utilize the system tray functionality.
- SystemTray
The SystemTray class represents the desktop system tray. - TrayIcon
The TrayIcon object represents a tray icon that can be added to the system tray.
How to create a blinking icon
To create a flashing icon, you may use the following site
Upload a local icon to create a flashing icon.
https://www.bannerkoubou.com/anime/
Sample Applications
The sample application uses the system tray utility class TaskTrayUtil to implement the following functions
- When activated.task trayindicates such things as location of person or thing, location of short-term action, etc.iconDisplay.
- Click the [Flashing Button],task trayicon blinks.
Also, a balloon with the message "Get me out of here!!!!!!!!!!!!!!!!!!!!!!!!" message. - Click [Release button] to cancel the icon blinking.
- task trayicon with a single left click to bring the main window out onto the desktop.
- task trayicon, right-click to display the default PopupMenu menu.
- Once the main window is minimizedtask trayStored in
Source code acquisition
The source code can be obtained from When compiling, please also save the following icons in the same location as the source coded.
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**
* Display/blink the task tray (system tray) icon in Java.
*
* ◆ Overview of this class
* The TaskTray class is a utility that handles the task tray.
* Provides the following features:
* ・Display the icon in the task tray.
* - Display a pop-up message in the task tray.
* - Make the task tray blink.
*
* ◆ How to use
* Pass the target JFrame class to the method argument below.
* ・createTray(JFrame targetFrame)
* ・createTray(final JFrame targetFrame, Image image, PopupMenu menu)
*
*/
public class TaskTrayUtil {
private static JFrame frame;
private static PopupMenu defaultMenu;
private static TrayIcon trayIcon;
private static SystemTray tray = SystemTray.getSystemTray();
private static Object INSTANCE_LOCK = new Object();
/**
* Execution sample
* @param args
*/
public static void main(String[] args) {
try {
// Set LookAndFeel to Windows
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
// regular icon
final Image img = new ImageIcon(
TaskTrayUtil.class.getResource(
“tray_icon.gif”)).getImage();
final JFrame frame =
new JFrame(“Display/blink icon in task tray”);
frame.setSize(300, 100);
frame.setIconImage(img);
// Store in task tray when window is minimized
// Add WindowListener class as shown.
frame.addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
frame.setVisible(false);
}
});
JPanel jPanel = new JPanel();
jPanel.setLayout(new FlowLayout());
jPanel.setSize(new Dimension(219, 70));
// blinking icon
final Image imgLight =
new ImageIcon(TaskTrayUtil.class.getResource(
“tray_icon_light.gif”)).getImage();
JButton lightButton = new JButton(“blink”);
lightButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
onAndOffLight(imgLight, null);
displayMessage(“Message”, “Get me out of here!!”);
}
});
JButton cancelButton = new JButton(“cancel”);
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
replaceImageWithDelete(img);
}
});
jPanel.add(lightButton, null);
jPanel.add(cancelButton, null);
frame.add(jPanel);
// End processing.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Display the frame in the center of the screen.
frame.setLocationRelativeTo(null);
// Create task tray.
TaskTrayUtil.createTray(frame);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
*/
static {
createPopupMenu();
}
/**
* Generates a default PopupMenu menu to be displayed in the task tray.
*The following menus are available.
* ・"Remove from task tray"
* Move the target application from the task tray to the desktop
* Take it out.
* (Activate the window and bring it to the front.)
* - Terminate the application to be terminated.
*/
private static void createPopupMenu() {
MenuItem getTrayItem = new MenuItem(“Remove from task tray”);
getTrayItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
active();
}
});
MenuItem exitItem = new MenuItem(“Exit”);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeTrayIcon();
TaskTrayUtil.frame.setVisible(false);
TaskTrayUtil.frame.dispose();
System.exit(0);
}
});
defaultMenu = new PopupMenu();
defaultMenu.add(getTrayItem);
defaultMenu.add(exitItem);
}
/**
* Create a tray icon and add it to the system tray.
* Set a listener for events that occur on the icon to targetFrame
Set to *.
* The event here is when the tray icon is double clicked.
* Extract the target application.
* @param targetFrame Target application
*/
public static void createTray(JFrame targetFrame) {
createTray(targetFrame, null, null);
}
/**
* Create a tray icon and add it to the system tray.
*
* @param targetFrame Target application
* @param image Image image to display on the tray icon.
* If null, it can be obtained from targetFrame with getIconImage()
* Use image images.
* @param menu PopupMenu to display in the task tray.
* If null, display the default PopupMenu.
*/
public static void createTray(final JFrame targetFrame, Image image,
PopupMenu menu) {
// Exit if system tray is not supported.
if (!SystemTray.isSupported()) {
return;
}
TaskTrayUtil.frame = targetFrame;
if (image == null) {
image = targetFrame.getIconImage();
}
if (menu == null) {
menu = defaultMenu;
}
trayIcon = new TrayIcon(image, targetFrame.getTitle(), menu);
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
active();
}
}
});
try {
tray.add(trayIcon);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
/**
* Remove icon from task tray.
* Must be called when the application exits.
*/
public static void removeTrayIcon() {
tray.remove(trayIcon);
}
/**
* Make the task tray icon blink.
* @param msg if necessary when the blinking icon is clicked
* Set the message to display.
*/
public static void onAndOffLight(Image lightImg, String msg) {
replaceImage(lightImg);
ChangeLightImgAdapter adap =
new TaskTrayUtil().new ChangeLightImgAdapter(msg);
trayIcon.addMouseListener(adap);
frame.addWindowListener(adap);
}
/**
* Change the task tray icon.
* @param image Change only if it differs from the current icon.
*/
public static void replaceImage(Image image) {
synchronized (INSTANCE_LOCK) {
if (!image.equals(trayIcon.getImage())) {
trayIcon.setImage(image);
}
}
}
/**
* Change the task tray icon.
* *If you want to restore the icon that is blinking for some reason,
* It seems to be useless unless you delete it with {@link SystemTray#remove(TrayIcon)}).
* @param image Change only if it differs from the current icon.
*/
public static void replaceImageWithDelete(Image image) {
synchronized (INSTANCE_LOCK) {
if (!image.equals(trayIcon.getImage())) {
tray.remove(trayIcon);
trayIcon.setImage(image);
try {
tray.add(trayIcon);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* Display a pop-up message in the task tray.
* @param caption
* @param text
*/
public static void displayMessage(String caption, String text) {
if (caption != null || text != null) {
trayIcon.displayMessage(caption, text,
TrayIcon.MessageType.INFO);
}
}
/**
* Move the target application from the task tray to the desktop
* Take it out.
*/
private static void active() {
// Return the frame state to normal.
TaskTrayUtil.frame.setExtendedState(JFrame.NORMAL);
TaskTrayUtil.frame.setAlwaysOnTop(true);
TaskTrayUtil.frame.setVisible(true);
TaskTrayUtil.frame.setAlwaysOnTop(false);
}
/**
* Event to be generated when the blinking icon is clicked
* Listener class.
*/
class ChangeLightImgAdapter implements WindowListener, MouseListener {
String lightImageText;
public ChangeLightImgAdapter(String lightImageText) {
this.lightImageText = lightImageText;
}
/**
* When the task tray icon is clicked
* Show popup message.
*/
public void mousePressed(MouseEvent e) {
TaskTrayUtil.displayMessage(null, lightImageText);
}
/**
* Blinked when the target application became active
* Return icon.
*/
public void windowActivated(WindowEvent e) {
TaskTrayUtil.replaceImageWithDelete(TaskTrayUtil.frame
.getIconImage());
TaskTrayUtil.frame.removeWindowListener(this);
TaskTrayUtil.trayIcon.removeMouseListener(this);
}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
}