Comment puis-je mettre une application Java dans la barre d'état système?

J'ai un petit panneau de contrôle, juste une petite application que j'ai faite. Je voudrais minimiser / mettre le panneau de commande Haut / Bas avec les systemicons, avec la durée de vie de la batterie, la date, les réseaux, etc.

Quelqu'un qui peut me donner un indice, un lien vers un tutoriel ou quelque chose à lire?

50
demandé sur Jim Puls 2009-04-17 01:13:09

3 réponses

Comme de Java 6, c'est pris en charge dans le SystemTray et TrayIcon des classes. SystemTray a un exemple assez étendu dans ses 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);
}
// ...

Vous pouvez aussi consulter cet article, ou cette astuce technique.

68
répondu Michael Myers 2014-10-06 16:52:06

C'est très 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

Définissez le chemin Approprié pour l'image, puis exécutez le programme. T. Y.:)

15
répondu Nilesh Jadav 2016-01-17 21:23:19

C'est le code que vous pouvez utiliser pour accéder et de personnaliser la barre d'état système:

final TrayIcon trayIcon;

if (SystemTray.isSupported()) {

SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");

MouseListener mouseListener = new MouseListener() {

    public void mouseClicked(MouseEvent e) {
        System.out.println("Tray Icon - Mouse clicked!");                 
    }

    public void mouseEntered(MouseEvent e) {
        System.out.println("Tray Icon - Mouse entered!");                 
    }

    public void mouseExited(MouseEvent e) {
        System.out.println("Tray Icon - Mouse exited!");                 
    }

    public void mousePressed(MouseEvent e) {
        System.out.println("Tray Icon - Mouse pressed!");                 
    }

    public void mouseReleased(MouseEvent e) {
        System.out.println("Tray Icon - Mouse released!");                 
    }
};

ActionListener exitListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        System.out.println("Exiting...");
        System.exit(0);
    }
};

PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
defaultItem.addActionListener(exitListener);
popup.add(defaultItem);

trayIcon = new TrayIcon(image, "Tray Demo", popup);

ActionListener actionListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        trayIcon.displayMessage("Action Event", 
            "An Action Event Has Been Performed!",
            TrayIcon.MessageType.INFO);
    }
};

trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);

try {
    tray.add(trayIcon);
} catch (AWTException e) {
    System.err.println("TrayIcon could not be added.");
}

} else {

//  System Tray is not supported

}
2
répondu Lakshya Goyal 2016-04-14 12:07:12