I have a menu bar class (MenuBarGUI) that I have put in all of my other classes and I need to know if I can close the current JFrame that it is contained in through an option in the menu. Normally I could call setVisible(false) then dispose() but because the menu bar doesn't have a reference to the current frame, I can't do that. Is there any way to do this?
Just create another class, let us say FrameMananger, then use the singleton pattern to manage them. Then in any class, you can use FrameManager. getFrame1() to get the frame1, same as the frame2. You can add logic judgement inside, like dynamically dispose some frame or only create them when needed.
To make the frame visible, invoke setVisible(true) on it.
We can implement most of the java swing applications using JFrame. By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class.
if your MenuBarGUI class extends JMenuBar, you can use getTopLevelAncestor method to get menu bar's window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With