简体   繁体   中英

Creating a popup window on button click using JPanel from another JPanel

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?

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.

use something like

if (evt.getModifiers() == MouseEvent.BUTTON3_MASK){  
    popup.show(evt.getComponent(), evt.getX(), evt.getY());//show popup 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM