简体   繁体   中英

Run Application in tray icon(not in taskbar)

I want to run my app in the tray but I don't the icon in the taskbar.

I using my JFrame in Hide_On_Close operation

and I using a tray icon for notifications but how to get rid of the icon on the taskbar while my GUI application running.

Does this work for you? You might need to add what os you're working on.

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class SysTrayFrame{

    public static void main(String[] args) throws Exception{
        JFrame frame = new JFrame("systray test");
        frame.setUndecorated(true);
        BufferedImage img =  new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.getGraphics();
        g.setColor(Color.RED);
        g.fillOval(0, 0, 64, 64);
        g.dispose();
        JButton b = new JButton("click to hide");
        frame.add(b);
        b.addActionListener(evt-> frame.setVisible(false) );
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        TrayIcon icon = new TrayIcon(img);
        icon.addActionListener( evt->{
            System.out.println("doing it");
            frame.setVisible(true);
        });
        SystemTray.getSystemTray().add(icon);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM