Question

I'm beginner in Java GUI and have a problem to design a popup menu. I have a JPanel and want to have a popup menu with just two alternatives. Here is my code:

        ActionListener ordermethod = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        };

        JPopupMenu popup = new JPopupMenu();

        JMenuItem arr = new JMenuItem("first");
        arr.addActionListener(ordermethod);
        popup.add(arr);
        arr = new JMenuItem("second");
        arr.addActionListener(ordermethod);
        popup.add(arr);
        myPanel.add(new JLabel("Pattern ordering method:"), gbc);
        myPanel.add(arr, gbc);

but the problem is that when I run the code, we see the JLabel and and a box that shows "second" and when I click on the "second" to find "first", get a long list of errors. (this is some parts of errors)

Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
at main.Main$1.actionPerformed(Main.java:53)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)

Hopefully the problem is clear and you can help me. Thanks.

Was it helpful?

Solution

Try removing the line throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. from you actionPerformed method...and replace it with your required application logic

ActionListener ordermethod = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, "Hello");
    }
};

JPopupMenu popup = new JPopupMenu();

JMenuItem arr = new JMenuItem("first");
arr.addActionListener(ordermethod);
popup.add(arr);
arr = new JMenuItem("second");
arr.addActionListener(ordermethod);
popup.add(arr);
myPanel.add(new JLabel("Pattern ordering method:"), gbc);
myPanel.add(arr, gbc);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top