Java Swing How to - Create a Popup Menu with Nested Menus








Question

We would like to know how to create a Popup Menu with Nested Menus.

Answer

//from w  w w .j a v a  2 s .  c  o m
import javax.swing.JMenu;
import javax.swing.JPopupMenu;

public class Main {
  public static void main(String[] argv) throws Exception {
    final JPopupMenu popupMenu = new JPopupMenu();

    JMenu submenu = new JMenu("SubMenu1");
    submenu.add("asdf");
    submenu.add("asdf");

    // Add submenu to popup menu
    popupMenu.add(submenu);
  }
}