0

I am using netbeans to create a GUI for a tool I am working on. The tools contents are contained in a class that extends JPanel and has a button. When I click the button, I want a window to pop up which will have additional buttons and options, the contents of which are defined in another class that also extends JPanel. How can I accomplish this?

Simplified code of main class. I removed all the code that is not important to this problem:

public class FirstPanel extends JPanel {

    private JButton myButton;

    public FirstPanel() {

        myButton = new JButton("Button");
        myButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {

                // TO DO

            }
        });
    }
}

And then my second class would look similar, and be in charge of handling all of its buttons and such. How can I accomplish this?

Duffluc
  • 27
  • 2
  • 6

2 Answers2

2

See How to Use Menus: Bringing Up a Popup Menu for the correct way to handle this in a cross-platform MouseListener. Even more simply, use setComponentPopupMenu(), as shown here. In either case, note the use of Action to encapsulate the desired functionality.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

use something like

if (evt.getModifiers() == MouseEvent.BUTTON3_MASK){  
    popup.show(evt.getComponent(), evt.getX(), evt.getY());//show popup 
}
Gavin
  • 8,204
  • 3
  • 32
  • 42
Tech Nerd
  • 822
  • 1
  • 13
  • 39
  • @user3280809: If you go this route, use [`isPopupTrigger()`](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html#popup) instead. – trashgod Mar 24 '14 at 19:48