Question

I have the following code to create a TrayIcon with a PopupMenu :

public void addToTray()
{
    try 
    {
        try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }

        PopupMenu popMenu= new PopupMenu();
        MenuItem exititem = new MenuItem("Exit");
        popMenu.add(exititem);

        BufferedImage trayImg = ImageIO.read(new File("Geqo.png"));
        ImageIcon ii = new ImageIcon(trayImg);
        TrayIcon trayIcon = new TrayIcon(ii.getImage(), "Geqo", popMenu);
        trayIcon.setImageAutoSize(true);
        SystemTray.getSystemTray().add(trayIcon);
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

This code is meant to create a TrayIcon with a PopupMenu. This works fine. But I did'nt like the default LAF (Metal, I think). So I tried to change the LAF to nimbus, as well as the OS Default, Windows, but to no avail. The LAF doesn't seem to be changing. Can anyone hguide me on how I chould change the LAF? Thanks in advance :)!!

Was it helpful?

Solution

Popup is not a Swing component (therefore it's doesn't come under the control of the LookAndFeel manager).

The Popup is an AWT component, which generally uses native components.

Instead, you should try something more like...

public void addToTray()
{
    try 
    {
        try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }

        BufferedImage trayImg = ImageIO.read(new File("Geqo.png"));
        ImageIcon ii = new ImageIcon(trayImg);
        final TrayIcon trayIcon = new TrayIcon(ii.getImage(), "Geqo", null);
        
        JPopupMenu jpopup = new JPopupMenu();
        JMenuItem miExit = new JMenuItem("Exit");
        jpopup.add(miExit);
        
        miExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                SystemTray.getSystemTray().remove(trayIcon);
                System.exit(0);
            }
        });
    
        trayIcon.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    jpopup.setLocation(e.getX(), e.getY());
                    jpopup.setInvoker(jpopup);
                    jpopup.setVisible(true);
                }
            }
        });
        
        trayIcon.setImageAutoSize(true);
        SystemTray.getSystemTray().add(trayIcon);
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

This is based on the idea suggested in Using JPopupMneu in TrayIcon

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top