Creating a java program is performed on the background of the computer

I looked at some other posts about this, but really didn’t quite understand.

I created a program that works like a server, while capturing different screen images. Now I want the program to simply be active in the background - for example, programs that appear under hidden icons. Programs that are not directly displayed on the lower taskbar. Do I need to add any specific code to my java program when I run it in a jar file? Or do I need to create a project in a different way?

enter image description here

enter image description here

Hope this was enough - Thanks in advance

+4
source share
3 answers

- , . , , .

public static void main (String [] args) {
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;
    }
    Image image = Toolkit.getDefaultToolkit().getImage("MY/PATH/TO_IMAGE");

    final PopupMenu popup = new PopupMenu();
    final TrayIcon trayIcon = new TrayIcon(image, "MY PROGRAM NAME", popup);
    final SystemTray tray = SystemTray.getSystemTray();

    MenuItem exitItem = new MenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(1);
        }
    });
    popup.add(exitItem);

    trayIcon.setPopupMenu(popup);

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

.

+1

, API java.awt.SystemTray Java Swing API.

Oracle:

Oracle Java API

0

SystemTray.getSystemTray().add(trayIcon) performs the task.

Here is an example of one of my applications:

Image imageTrayIcon = Toolkit.getDefaultToolkit().createImage(YourClass.class.getResource("trayicon.png"));
    final TrayIcon trayIcon = new TrayIcon(imageTrayIcon, "title");

   // optional : a listener
    trayIcon.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {

        if (e.getClickCount() == 2 && !e.isConsumed()) {
            e.consume();
           // process  double click
           }
        }
    });
    // optional : adding a popup menu for the icon
    trayIcon.setPopupMenu(popup);

    // mandatory
    try {
        SystemTray.getSystemTray().add(trayIcon);
    }
    catch (AWTException e1) {
        // process the exception
    }
0
source

Source: https://habr.com/ru/post/1651579/


All Articles