Problems with JPanel exchange within JDialog using Swing

4

I'm having a problem with a system I'm developing with regard to screen switching. Sorry for the lengthy post, but I could not explain the problem otherwise.

It has a JFrame principal with a menu that is JButtons and a JMenuBar . One of these JButtons opens a JDialog with other JButtons that when I click, it redraws a new JPanel instead of JPanel of that JDialog .

The JMenuBar that is in JFrame main represents the same thing as the JButtons in the menu because it is just an alternative way to navigate the system. I have the same thing representing these menus, the details of the JMenuBar structure is not relevant.

Then as I said, when I click on a JButton of JPanel that is in the JFrame main, it opens a JDialog that has a JPanel inside it with another menu of JButtons. p>

When I click on one of these JButtons, it performs the redrawing of a new JPanel within that JDialog using the similar code below (the code below is inside a class that extends JDialog ) using the JDialog instance % created in the previous submenu, since in this mode I will access the screen doing a redesign of JPanel taking advantage of the existing instance JDialog :

    getContentPane().removeAll();
    getContentPane().add(new JScrollPane(jPanelQualquer));
    repaint();
    printAll(getGraphics());

Now, when I access the same screen with JMenuBar , as I do direct access to the screen, I do not redraw JPanel , but I open a new window using an algorithm similar to the one below class that extends JDialog ) where in this case a new instance of JDialog has been created using new JDialog() :

    setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
    setModalityType(ModalityType.MODELESS);
    setLocationRelativeTo(principalFrame);
    getContentPane().add(new JScrollPane(jPanelQualquer));
    setVisible(true);

I'm having a bug in this scenario that I have no idea what it can be because Java simply does not generate any errors. It just does not perform what it is to run.

What I do is, first access to the application, access the submenu by JButton of the main menu, and access the screen by way of redrawing JPanel through the submenu. So far ball show because the screen is redesigned. Then I close the JDialog of the submenu, and access that submenu again by the JButton of the main menu, and with JDialog in the submenu open, I go there in the menu bar JMenuBar and access the screen directly (so it creates a new JDialog already with the drawn canvas, that is, I am with JDialog with JPanel of the screen and another JDialog with JPanel of submenu with JButton which redraws JDialog with that same JPanel of the screen). Until then, okay too. I close the JDialog with the JPanel of the screen, and with the other JDialog still open of the submenu I try to access the same screen again (remembering that this submenu redraws JPanel in JDialog of it instead of creating a new instance).

Result: Nothing happens.

What can it be? I have debugged already and confirmed by the algorithm that when I do this, the instance of JPanel that is to draw it is intact, it does not merely redraw JDialog .

If you instantiate a new JDialog using the same JPanel that, after closing, another JDialog you want to use, can influence something?

    
asked by anonymous 09.07.2015 / 22:51

1 answer

2

Daniel

According to the documentation ( did not find the article ) component may not be appearing in two places at the same time.

  

Each GUI component can be contained only once. If a component is   already in a container and you try to add it to another container, the   component will be removed from the first container and then added to   the second.

In your case I think it's because JDialog still exists, it's just not showing. Then, to display the same JPanel in another JDialog , you will need a new instance of it.

    
13.07.2015 / 21:03