简体   繁体   中英

Dynamic Menu Java Swing

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.

public class MenuPrincipal extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
public static JDesktopPane desktop;

public MenuPrincipal(ArrayList<MenuDTO> 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 < 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);
}
}

Consider implementing the Action interface, as discussed in How to Use Actions . There are related examples here and here . You can use Class Literals as Runtime-Type Tokens to get a newInstance() of each of your classes.

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