I have a little control-panel, just a little application that I made. I would like to minimize/put the control-panel up/down with the systemicons, together with battery life, date, networks etc.
Anyone that can give me a clue, link to a tutorial or something to read?
The SystemTray class represents the system tray for a desktop. On Microsoft Windows it is referred to as the "Taskbar Status Area", on Gnome it is referred to as the "Notification Area", on KDE it is referred to as the "System Tray". The system tray is shared by all applications running on the desktop.
The system tray provides quick access to applications: to open a program window, double-click on the relevant icon with the left mouse button; for other actions, click once with the right mouse button.
As of Java 6, this is supported in the SystemTray
and TrayIcon
classes. SystemTray
has a pretty extensive example in its Javadocs:
TrayIcon trayIcon = null; if (SystemTray.isSupported()) { // get the SystemTray instance SystemTray tray = SystemTray.getSystemTray(); // load an image Image image = Toolkit.getDefaultToolkit().getImage("your_image/path_here.gif"); // create a action listener to listen for default action executed on the tray icon ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { // execute default action of the application // ... } }; // create a popup menu PopupMenu popup = new PopupMenu(); // create menu item for the default action MenuItem defaultItem = new MenuItem(...); defaultItem.addActionListener(listener); popup.add(defaultItem); /// ... add other items // construct a TrayIcon trayIcon = new TrayIcon(image, "Tray Demo", popup); // set the TrayIcon properties trayIcon.addActionListener(listener); // ... // add the tray image try { tray.add(trayIcon); } catch (AWTException e) { System.err.println(e); } // ... } else { // disable tray option in your application or // perform other actions ... } // ... // some time later // the application state has changed - update the image if (trayIcon != null) { trayIcon.setImage(updatedImage); } // ...
You could also check out this article, or this tech tip.
It's very simple
import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; public class SystemTrayDemo{ //start of main method public static void main(String []args){ //checking for support if(!SystemTray.isSupported()){ System.out.println("System tray is not supported !!! "); return ; } //get the systemTray of the system SystemTray systemTray = SystemTray.getSystemTray(); //get default toolkit //Toolkit toolkit = Toolkit.getDefaultToolkit(); //get image //Toolkit.getDefaultToolkit().getImage("src/resources/busylogo.jpg"); Image image = Toolkit.getDefaultToolkit().getImage("src/images/1.gif"); //popupmenu PopupMenu trayPopupMenu = new PopupMenu(); //1t menuitem for popupmenu MenuItem action = new MenuItem("Action"); action.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Action Clicked"); } }); trayPopupMenu.add(action); //2nd menuitem of popupmenu MenuItem close = new MenuItem("Close"); close.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); trayPopupMenu.add(close); //setting tray icon TrayIcon trayIcon = new TrayIcon(image, "SystemTray Demo", trayPopupMenu); //adjust to default size as per system recommendation trayIcon.setImageAutoSize(true); try{ systemTray.add(trayIcon); }catch(AWTException awtException){ awtException.printStackTrace(); } System.out.println("end of main"); }//end of main }//end of class
Set appropriate path for image and then run the program. t.y. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With