Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic Menu Java Swing
    text
    copied!<p>I need to make a dynamic menu in java swing. I have a database table that has a menu structure. This is my class that assembles the menu, but I want to avoid burning in the literal code in the actionPerformed method, the class name (screen) comes in a table field. My screens are one destoktopPane.</p> <pre><code>public class MenuPrincipal extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; public static JDesktopPane desktop; public MenuPrincipal(ArrayList&lt;MenuDTO&gt; opcionesUsuario) { //Creamos la ventana del menu principal. super("Menu Principal - Demografo"); JMenuBar menuBar = null; JMenu menu = null, submenu = null; JMenuItem menuItem = null; int longitud = opcionesUsuario.size(); //Creamos una barra de menu menuBar = new JMenuBar(); menu = new JMenu("Archivo"); menuItem = new JMenuItem("Cerrar sessión", new ImageIcon("images/middle.gif")); menu.add(menuItem); menu.addSeparator(); menuItem = new JMenuItem("Salir"); menu.add(menuItem); MenuDTO menuDTO; for(int i = 0; i &lt; longitud; i ++) { menuDTO = opcionesUsuario.get(i); if(menuDTO.getTieneHijos().equals("S")) { if(menuDTO.getIdPadre() == null) { menu = new JMenu(menuDTO.getNombre()); menuBar.add(menu); } else { submenu = new JMenu(menuDTO.getNombre()); menu.add(submenu); } } else { menuItem = new JMenuItem(menuDTO.getNombre()); menuItem.setActionCommand(Integer.toString(menuDTO.getIdOpcion())); menuItem.addActionListener(this); menu.add(menuItem); } } desktop = new JDesktopPane(); desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); int inset = 50; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds(inset, inset, screenSize.width - inset*2, screenSize.height - inset*2); setContentPane(desktop); setJMenuBar(menuBar); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setExtendedState(MAXIMIZED_BOTH); setSize(450, 260); setVisible(true); } public void actionPerformed(ActionEvent e) { if ("4".equals(e.getActionCommand())) IngresoCiudades.getInstance(desktop); else if ("5".equals(e.getActionCommand())) IngresoParroquias.getInstance(desktop); else if ("8".equals(e.getActionCommand())) IngresoBarrios.getInstance(desktop); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload