Click here to Skip to main content
15,946,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm create Popup Menu without jframe, for TryIcon. It all action are working, but does,t work Menu Item selection mode and popup menu invisible function. how do it?

Java
class MyPopupMenu {

    private JPopupMenu popupMenu = null;
    private JMenuItem jMenuItem1 = null, jMenuItem2 = null;
    private JMenuItem jMenuItem3 = null, jMenuItem4 = null;
    private JMenuItem jMenuItem5 = null;

    private void createMenu() {
        popupMenu = new JPopupMenu();
        jMenuItem1 = new JMenuItem("Show Player");
        jMenuItem2 = new JMenuItem("Play");
        jMenuItem3 = new JMenuItem("Pause");
        jMenuItem4 = new JMenuItem("Stop");
        jMenuItem5 = new JMenuItem("Exit");
    }

    private void ViewMenu() {
        initializeMenuItems();
        popupMenu.add(jMenuItem1);
        popupMenu.add(jMenuItem2);
        popupMenu.add(jMenuItem3);
        popupMenu.add(jMenuItem4);
        popupMenu.add(jMenuItem5);
        popupMenu.show(null, 0, 0);
    }

    private void initializeMenuItems() {
        createMenu();
        jMenuItem1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("A");
            }
        });
        jMenuItem2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("B");
            }
        });
        jMenuItem3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("C");
            }
        });
        jMenuItem4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("D");
            }
        });
        jMenuItem5.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String chi[]) {
        new MyPopupMenu().ViewMenu();
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900