0

I want to create a System Tray using JPopupMenu however it only accepts awt's PopupMenu.

I use the following code:

package UserInterface;

import java.awt.AWTException;
import java.awt.Font;
import java.awt.Image;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class StartBarMenu extends JPopupMenu {
    private static final long serialVersionUID = 1L;

    public StartBarMenu() {
        JMenuItem mntmTwincloud = new JMenuItem("Cloud");
        mntmTwincloud.setFont(new Font("Segoe Print", Font.BOLD, 14));
        this.add(mntmTwincloud);

        JMenuItem mntmCapacity = new JMenuItem("X GB of Y GB used");
        this.add(mntmCapacity);

        this.addSeparator();

        JMenuItem mntmOpenTwinCloud = new JMenuItem("Open Cloud folder");
        this.add(mntmOpenTwinCloud);

        JCheckBoxMenuItem chckbxmntmEnableAutoSync = new JCheckBoxMenuItem(
                "Enable Auto Sync");
        this.add(chckbxmntmEnableAutoSync);
        chckbxmntmEnableAutoSync.setState(true);

        JMenuItem mntmShareFile = new JMenuItem("Share File");
        this.add(mntmShareFile);

        JMenuItem mntmLogOut = new JMenuItem("Log Out");
        this.add(mntmLogOut);

        JMenuItem mntmExit = new JMenuItem("Exit");
        this.add(mntmExit);

        addPopUpToTry();
    }

    private void addPopUpToTry() {
        final SystemTray tray = SystemTray.getSystemTray();
        final TrayIcon trayIcon = new TrayIcon(createImage("bulb.gif", "tray icon"));
        trayIcon.setPopupMenu(this); // Error

        trayIcon.setImageAutoSize(true);

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

    protected static Image createImage(String path, String description) {
        URL imageURL = TrayIconDemo.class.getResource(path);

        if (imageURL == null) {
            System.err.println("Resource not found: " + path);
            return null;
        } else {
            return (new ImageIcon(imageURL, description)).getImage();
        }
    }
}

However, trayIcon.setPopupMenu(this); gives the following error.

The method setPopupMenu(PopupMenu) in the type TrayIcon is not applicable for the arguments (JPopupMenu)

When I change the JPopupMenu to awt PopupMenu and do other necessary changes I can run the code successfully. But I do not want to use PopupMenu.

How I can use JPopupMenu and put it into System Tray?

user2640782
  • 1,074
  • 8
  • 15
  • `TrayIcon` is an AWT component, so it doesn't support the Swing components directly, there's a trick you can use where by you monitor the `TrayIcon`'s `MouseEvents` an popup the menu yourself, for [example](http://stackoverflow.com/questions/14670516/how-do-i-get-a-popupmenu-to-show-up-when-i-left-click-on-a-trayicon-in-java/14681503#14681503) – MadProgrammer Mar 06 '16 at 20:20
  • Thank you, I followed your example and solved the problem. If you want you can write the solution as an answer and I can accept it. – user2640782 Mar 07 '16 at 11:01

0 Answers0