I have Main class, StartFrame extends JFrame, and UserPanel extends JPanel which I add to StartFrame. I have button in the UserPanel, how can I close StartFrame when I press the button(I am familiar with event handling it’s not a problem the issue is how to sent info to the StartFrame) . Or it is better to just change the panel of the frame(size if need) and reuse it?
I have Main class, StartFrame extends JFrame, and UserPanel extends JPanel which I add
Share
If you want to close a window that is enclosing a component, you need a reference to that Window, and SwingUtilities has a method that can help you get this:
getWindowAncestor(Component c). Then you could calldispose()on the window returned.i.e.,
Note that this is fine if you’re using this to end your GUI, but if your goal is to swap views, then a better suggestion is not to swap Windows, but rather to leave the main JFrame visible but to instead swap components it shows with a CardLayout.