Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get a reference to the current frame in Java?

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?

like image 862
LOD121 Avatar asked Feb 01 '11 22:02

LOD121


People also ask

How do you link two frames in Java?

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.

How do you make a frame visible in Java?

To make the frame visible, invoke setVisible(true) on it.

Which method will case AJ frame to get displayed?

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.


1 Answers

if your MenuBarGUI class extends JMenuBar, you can use getTopLevelAncestor method to get menu bar's window.

like image 175
Gursel Koca Avatar answered Sep 21 '22 06:09

Gursel Koca