Question

Is there a way to render graphics like JPanel in a PopupMenu (with TrayIcon)? I know it's possible by using JPopupMenu but I do not like that the popup doesn't close if I click outside of it (and the icon does not get highlighted as with PopupMenu).

Example of what I'm trying to do with JPopupMenu:

    if( SystemTray.isSupported() ) {
        // Get the SystemTray instance
        SystemTray tray = SystemTray.getSystemTray();

        // Load icon
        Image image = new ImageIcon(this.getClass().getResource("delete.png")).getImage();

        final JPopupMenu popup = new JPopupMenu();
        popup.add( new JMenuItem("Test") );

        JPanel p1 = new JPanel();
        p1.setBackground( Color.red );
        p1.setPreferredSize( new Dimension(200, 300) );
        popup.add( p1 );

        JTrayIcon trayIcon = new JTrayIcon( image );
        trayIcon.setJPopupMenu( popup );

        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                popup.setLocation(e.getX(), e.getY());
                popup.setInvoker(popup);
                popup.setVisible(true);
            }
        });

        try {
            tray.add( trayIcon );
        } catch (Exception e) {
            JOptionPane.showMessageDialog( null, "Could not add tray icon." );
        }
    }
Was it helpful?

Solution 2

You can extending JPopupMenu and add customItem to it:
public class CustomPopUp extends JPopupMenu {

    public CustomPopUp() {
        reload();
    }

    private void reload(final Collection<CustomItem> items) throws BadLocationException {
        for (final CustomItem item : items) {
            add(new AbstractAction(item.getLabel(), item.getIcon()) {               
                @Override
                public void actionPerformed(final ActionEvent e) {
                    //do whatever
                }
            });
        }

    }
}
public class CustomItem {
    private String label;
    private ImageIcon icon;

    //getter and setter
}

OTHER TIPS

Is there a way to render graphics like JPanel in a PopupMenu? I know it's possible by using JPopupMenu but I do not like that the popup doesn't close if I click outside of it (and the icon does not get highlighted as with PopupMenu).

  • I'll talking only about Java-2D directly to popup container, sure there no issue to put JPanel with custom painting, with JButtons, layed by GridLayout

  • Yes there are a few ways, the best describtion around by @Kirill Grouchnikov

  • you can to decide if you'll create

    1) a new paint for each of JPopupMenu / JMenu,

    2) put to the UIManager (then valid for all Objects in current JVM)

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