Problems with changing Jpanel inside Jdialog using Swing

Asked

Viewed 243 times

4

I’m having a problem with a system I’m developing regarding screen swapping. Sorry for the extensive post, but I could not explain the problem otherwise.

He owns a JFrame main with a menu that are Jbuttons and a JMenuBar. One of those Jbuttons opens a JDialog with other Jbuttons that when I click, it redesigns a new JPanel in place of JPanel current of that JDialog.

The JMenuBar who is in the JFrame main represents the same thing as the Jbuttons in the menu as it is just an alternative way of navigating the system. I have the same thing representing these menus, the details of the JMenuBar is not relevant.

So like I said, when I click on a JButton of JPanel who is in the JFrame main, it opens a JDialog who owns a JPanel inside it with another menu of Jbuttons.

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

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

Already, when I access the same screen by JMenuBar, how do I directly access the screen, I do not do a redesign of JPanel, and yes I open a new window using an algorithm similar to the one below (in the same class that extends JDialog) in which case a new instance of JDialog using the 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 might be because Java just doesn’t make any mistakes. It just doesn’t execute what it is to run.

What I do is, first access the application, access the submenu by JButton from the main menu, and access to the screen by way of redesigning the JPanel through the submenu. Until then show ball because the screen is redesigned. Then what I do is close the JDialog submenu, and access this submenu again by JButton from the main menu, and JDialog in the open submenu, I go there in the menu bar JMenuBar and screen access directly (thus it creates a new JDialog already with the drawn screen, IE, I’m with a JDialog with the JPanel screen and other JDialog with the JPanel of the submenu with the JButton that redesigns the JDialog with that same JPanel from the screen). So far ok too. Close the JDialog with the JPanel of the screen, and with the other JDialog still open submenu trying to access the same screen again (remembering that this submenu redesigns the JPanel in the JDialog instead of creating a new instance).

Result: Nothing happens.

What can it be? I debugged and confirmed by the algorithm that when I do this, the instance of JPanel which is for him to draw is intact, he merely does not redesign the JDialog.

The fact that a new JDialogusing the same JPanel that, once closed, another JDialog want to use, can influence on something?

  • Daniel. A component cannot be displayed in two places at the same time. If you want to display the same Jpanel in another Jdialog you will need a new instance of it. I can’t explain to you exactly why, I read this in an article. I’ll research it here and I’ll point it out. In your case I believe it is because the Jdialog still exists, only it is not being displayed.

  • But I believe you can do the following: jdialog.remove(jpainel); before closing it. Oh yes I believe you may be exhibiting it in another JDialog

  • @Danielchirattoseabra, already managed to resolve the issue?

  • @Dener, perfect placement! I did the test and in fact, the problem was trying to reuse the same Jpanel object. Creating a new instance solves the problem. Thank you very much!

  • Ok. I will turn in response to the post then! Glad I helped!

  • @Danielchiurattoseabra ready, if you have solved it I believe you can mark as answer.

  • @Dener, I’m scoring! Valeus boy!

Show 2 more comments

1 answer

2


Daniel

According to the documentation (I didn’t find the article) component cannot be being displayed 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 believe it’s because the JDialog still exists, only is not being displayed. So to display the same JPanel in another JDialog, you will need a new instance of it.

Browser other questions tagged

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