TokTok123
TokTok123

Reputation: 771

JFrame - Icon not showing on Sys tray when JFrame has been minimised

I'm currently dealing with minimizing a JFrame to the system tray and I've done so successfully as indicated below:

    // 
    URL resource = panel.getClass().getClassLoader().getResource("boston.png");
    System.out.println("rfc95Panel.getClass().getClassLoader().getResource() is: " + rfc95Panel.getClass().getClassLoader().getResource("boston.png"));

    Image image = Toolkit.getDefaultToolkit().getImage(resource);

    //
    frame.setIconImage(image);

    // 
    if (SystemTray.isSupported()) {
        final TrayIcon icon = new TrayIcon(image);
        icon.setToolTip("Program minimised");

        // 
        icon.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(true);
                frame.setExtendedState(frame.NORMAL);
                getSystemTray().remove(icon);
            }
        });

        // Adds the specified window state listener to receive window events 
        // from this window. If l is null, no exception is thrown and no action 
        // is performed.
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowIconified(WindowEvent e) {
                frame.setVisible(false);
                try {
                    getSystemTray().add(icon);
                } catch (AWTException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

The problem is when minimised I can't see the image/icon when I open the system tray i.e. I can see the tool tip but I can't see the icon related to my program - see image below

sys tray image

Any ideas as to what I'm missing? I feel it might be something basic.

Upvotes: 0

Views: 302

Answers (1)

TokTok123
TokTok123

Reputation: 771

The simple answer to the above problem I was encountering was the size of the image I intended to use as the sys tray icon. Basically my dimensions were 64 by 64 but ideally it should be that the dimensions are smaller in my case I reduced the image to 16 by 16.

Upvotes: 1

Related Questions