5

I've looked on some other posts about this, but didn't really understand much from it.

I've made a program that works like a server while capturing different pictures of the screen. Now, i'd like the program to just be active in the background - like the programs that appear under hidden icons. Programs that are not directly shown at the bottom taskbar. Do i need to add some specific code inside my java program when i execute it to a jar file? Or do i need to create the project some other way?

enter image description here

enter image description here

I hope this was enough explanation - Thanks in advance

6
  • You don't need special things for it. Creating a executable jar is enough. Commented Aug 17, 2016 at 16:46
  • " like the programs that appear under hidden icons." What is it ??? Commented Aug 17, 2016 at 16:47
  • And if you want to start it from the console but not associate it with one you can simply use javaw instead of java. Commented Aug 17, 2016 at 16:48
  • So you want the jar file to run in the background but you also want it to hidden so the user can only see it if they have set the folder options to "Show Hidden Files" ? @Ander Lassen Commented Aug 17, 2016 at 16:49
  • @BSD_ ah, no sorry - poor explanation. I want it to be shown when you open up the little icon that shows programs that run on the computer, but are not directly shown at the windows taskbar. See picture that i added. Commented Aug 17, 2016 at 16:51

3 Answers 3

3

Something super simple that I got from Here. All I did was add an exit on click.

Code

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.");
    }
}

Just get any image and add it to your resources or wherever you keep your images and make a path to it.

Sign up to request clarification or add additional context in comments.

Comments

1

You can achieve this by using the java.awt.SystemTray API in combination with Java Swing API.

Refer this documentation from Oracle:

Oracle Java documentation for System Tray API

Comments

0

SystemTray.getSystemTray().add(trayIcon) does the job.

Here an example of one of my application :

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
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.