Open Panel through a Jmenuitem

Asked

Viewed 352 times

1

I need to know how to make one JMenuItem open a JPanel, that is, by clicking on JMenuItem, open the corresponding Jpanel, this in Eclipse, via windowBuilder.

This is the image of my screen with the menu:

!Screenshot of the screen with the menu1

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 complete code here: http://pastebin.com/6rn7sSG6

  • How are you building your window? The jpanel will open where? To make it easier to help, edit the question and add a [mcve] because it is so wide.

  • http://postimg.org/image/69bpmqibd/ I want the edit button upstairs to open the panelEdit - tell me if you need more data

  • Add to the question, with code too, it is difficult to simulate a problem per image.

  • Rui, add to question, by clicking [Edit]. Here in the comments it is embossed. Add both the image and the code and format the code by selecting it and clicking on {}

  • You want to change the display panel to the panelEdit ?

  • yes by clicking Edit(jmenuitem)

  • Which panel do you want to add? The Edit pane has already been added. Your question is too confusing. Edit it explaining better, with a code more according to your doubt.

  • I don’t want to add any panel. I want that when running the program the user click event -> edit and open the panelEditar, the image is showing another panel, and when I run I can’t switch to another 'page' or panel, if you can help thank

  • So the problem is not in this code. In it you are already displaying the panel. Edit the question and add a code more according to your problem.

  • Because I also thought that this part of the code is well done.... I don’t know where else I can be wrong... I can’t find any example where someone does this to see what I have to change... and my code has 300 lines I don’t know that I show you more...

  • To change the panel displayed, see the code of that answer

  • Is the mistake in the part I put in now?

  • Which panel is displayed when opening the window?

  • http://postimg.org/image/qy1yzgoc9/

  • Try panelEditar.setVisible(true) within the ActionListener

  • did not work :(

  • Rui, see if you can solve the answer below.

  • did not resolve :(

Show 13 more comments

1 answer

0


The error is here:

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

The name you’re giving to your dashboard is name_5905325036674, and not painelEditar.

Either you change the name in the line above, or you switch to ActionListener, with the name already provided:

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

Reference:

/a/4112/28595

  • I’ve done it and it still doesn’t work

  • @Ruimendes can not simulate if the problem is elsewhere if you do not add a [mcve]. Try to add a piece of code that is possible to simulate your code, to check where the problem is.

  • https://www.zipshare.com/download/eyJhcmNoaXZlSWQiOiJhM2JlZDBhMS1iZTg0LTQ2NzYtYTk0OC1hYjQwNzhiZjE2MTEifQ== link to load the whole code... also not big... I am in this problem since yesterday

  • @Ruimendes posts the entire code on Pastebin or other code site.

  • http://pastebin.com/6rn7sSG6 - ja esta, did not know the site :p

  • @Ruimendes has no problem with his code. What happens is that the panelEditar was started but you did nothing else with it, has no element. Add a component to it so you can see that it’s working as the answer tells you.

  • http://pastebin.com/ASrD2Z83 added a button to the panelEditar, but it did not solve the following error:http://postimg.org/image/tooa64q7/

  • @Ruimendes as I said, has no problem with your code. It normally ran here with the edit being changed.

  • then how come I can’t run :( in class I could switch between panels with the interface running through the menubar, here I can’t

  • Do the following, create a new empty class in your project, copy all that code from Pastebin and put it into it (beware of the different class name), and run only this new class, not the project. You’ll see that it works.

  • voila - is it common for this to happen of having to create another page? thank you very much for the patience and everything else :)

  • @Ruimendes may be a problem with your IDE. Talk to your teacher about this problem, maybe it will help you fix some configuration problem.

  • because.... he only responds during the week XD agr I change between these 2 projects the code until the program is complete - thanks @Diego F

  • @Ruimendes It would be interesting to mark the answers as accepted by clicking on v to serve as reference for others with similar doubt, after all, one of the problems that would prevent opening the panel is the answer :)

  • 1

    1st day :p discount

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.