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 JDialog
using 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.
– Dener
But I believe you can do the following:
jdialog.remove(jpainel);
before closing it. Oh yes I believe you may be exhibiting it in anotherJDialog
– Dener
@Danielchirattoseabra, already managed to resolve the issue?
– Reginaldo Soares
@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!
– Daniel Chiuratto Seabra
Ok. I will turn in response to the post then! Glad I helped!
– Dener
@Danielchiurattoseabra ready, if you have solved it I believe you can mark as answer.
– Dener
@Dener, I’m scoring! Valeus boy!
– Daniel Chiuratto Seabra