JMenuItem does not work correctly in JFrame

0

I have a window created with a JMenuBar placed at the top. Until then I had 2 JMenuItems that worked as a button and opened another frame. Until then everything works correctly. Today, I have the need to put another item to that menu, and following the logic of the previous two I implemented it. After a couple of hours looking at why it can be (it will be the biggest bullshit in the world) I have not managed to find out why this third item of the menu does not come to me.

JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenuItem mntmNewMenuItem = new JMenuItem("A\u00F1adir planificado");
        mntmNewMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                AñadirView v=new AñadirView();
                v.setVisible(true);
            }
        });

        JMenuItem mntmActualizar = new JMenuItem("Actualizar");
        mntmActualizar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                ArrayList<Camion>cm=(ArrayList<Camion>) camiones.getPlanif();
                list.removeAll();
                for(int i=0;i<cm.size();i++) {
                    list.add(cm.get(i).toString());
                }
            }
        });
        menuBar.add(mntmActualizar);
        menuBar.add(mntmNewMenuItem);

        JMenuItem mntmVerPlanifSemanal = new JMenuItem("Ver planif semanal");
        menuBar.add(mntmVerPlanifSemanal);

As you can see the last item is still not implemented, it is the one that does not work. I leave a capture here of my (primitive) interface so you can see it easily.

    
asked by Adrián 20.12.2018 в 12:39
source

1 answer

0

This example allows you to execute the action on each menuItem and so on.

public void ventanaMenu(){
    System.out.println("ventana"); // Creando la ventana de alto Nivel contenedor de todos los elementos
    JFrame ventana = new JFrame();
    ventana.setTitle("PRUEBAS");
    Dimension d = new Dimension(500, 500);
    ventana.setSize(d);
    ventana.setPreferredSize(d);


        JMenuBar barra = new JMenuBar(); // Se crea la barra de menu
        JMenu menuPopUp = new JMenu(); // popUp que mostrara los item
        menuPopUp.setText("Archivo");


            JMenuItem menu1 = new JMenuItem("Menu 1"); // MenuItem 1
            menu1.addActionListener(new ActionListener() { // Agregando el Listener
                @Override
                public void actionPerformed(ActionEvent e) { // Ejecutando la accion del click sobre el item
                    System.out.println("Accion menu 1");
                }
            });

            JMenuItem menu2 = new JMenuItem("Menu 2"); // MenuItem 2
            menu2.addActionListener(new ActionListener() {// Agregando el Listener
                @Override
                public void actionPerformed(ActionEvent e) {// Ejecutando la accion del click sobre el item
                    System.out.println("Accion menu 2");
                }
            });

            JMenuItem menu3 = new JMenuItem("Menu 3"); // MenuItem 3
            menu3.addActionListener(new ActionListener() {// Agregando el Listener
                @Override
                public void actionPerformed(ActionEvent e) {// Ejecutando la accion del click sobre el item
                    System.out.println("Accion menu 3");
                }
            });


    menuPopUp.add(menu1); // Agregando cada MenuItem al popUp del menu
    menuPopUp.add(menu2);
    menuPopUp.add(menu3);

    barra.add(menuPopUp); // Agregando el popUpMenu a la barra
    barra.setPreferredSize(new Dimension(300, 100)); // Estableciendo unas dimensiones para hacerla visible

    ventana.add(barra); // Se Agrega al contenedor de alto nivel JFrame

    ventana.pack();
    ventana.setVisible(true);

}
    
answered by 20.12.2018 в 14:40