Open Panel via a JMenuItem

1

I need to know how I can JMenuItem open JPanel , that is, by clicking JMenuItem , open the corresponding JPanel, this in Eclipse, through windowBuilder.

This is the image of my screen with the menu:

Image of the screen with the menu 1

JPanel call code:

    public static void main(String[] args) {
     EventQueue.invokeLater(new Runnable() {
      public void run() {
       try {
        Agenda1 window = new Agenda1();
        window.frame.setVisible(true);
       } catch (Exception e) {
        e.printStackTrace();
       }
      }
     });
    }

    /**
     * Create the application.
     */
    public Agenda1() {
     initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
      frame = new JFrame();
      frame.setBounds(100, 100, 450, 300);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().setLayout(new CardLayout(0, 0));
      -- -- -

      JPanel panelEditar = new JPanel();
      frame.getContentPane().add(panelEditar, "name_5905325036674");
      panelEditar.setLayout(new CardLayout(0, 0));
      -- -- --

      JMenuItem mntmEditar = new JMenuItem("Editar");
      mntmEditar.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent arg0) {
        CardLayout card = (CardLayout) frame.getContentPane().getLayout();
        card.show(frame.getContentPane(), "panelEditar");

       }


      });

The full code here: link

    
asked by anonymous 17.04.2016 / 22:12

1 answer

0

The error is here:

frame.getContentPane().add(panelEditar, "name_5905325036674");

The name you are giving to your dashboard is name_5905325036674 , not painelEditar .

Or you change the name in the line above, or change in the ActionListener , with the name already informed:

card.show(frame.getContentPane(), "name_5905325036674");

Reference:

link

    
17.04.2016 / 23:52