Netbeans JPopupMenu Issues

362 Views Asked by At

I made a popup menu in Netbeans and I want to make a menu item that sends me to another GUI when I press it, but I don't know how.

I have to do something like a restaurant menu and when someone presses the button from the menu to send them to a specific type of food.

I made it only to popup when I right click it.

private void formMousePressed(java.awt.event.MouseEvent evt) {
  if(evt.isPopupTrigger()){
      jPopupMenu1.show(evt.getComponent(),evt.getX(),evt.getY());
  }
}                                 

private void formMouseReleased(java.awt.event.MouseEvent evt) {                                   
    if(evt.isPopupTrigger()){
      jPopupMenu1.show(evt.getComponent(),evt.getX(),evt.getY());
  }
}   
1

There are 1 best solutions below

3
On

"I want to make a menu item that sends me to another GUI when I press it, but I don't know how."

I'm, not sure how the drag and drop works with GUI Builder for JPopupMenu. I tried to to drap and drop it, but it won't show the display, so I couldn't just drag and drop JMenuItems to it. So I had to hand code it.

  • I dragged a JPopupMenu to the frame (jPopupmenu1);
  • In the constructor, I added a JMenuItem to it.
  • I added an ActionListener to the JMenuItem
  • Just show instantiate the second GUI , and maybe dispose of the second, depending on your preference

public NewJFrame() {
    initComponents();
    JMenuItem item1 = new JMenuItem("Open GUI2");
    jPopupMenu1.add(item1);
    item1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            new GUI2();
        }
    });
}

....

private void formMousePressed(java.awt.event.MouseEvent evt) {                                  
        if (evt.isPopupTrigger()) {
            jPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }                                 

private void formMouseReleased(java.awt.event.MouseEvent evt) {                                   
        if (evt.isPopupTrigger()) {
            jPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }

It works fine for me

enter image description here