-1

Lets say I have the following structure.

enter image description here

Actually the red one is JDialog. My question is how to get the black main frame inside of the red jdialog so that I can change something of the black frame. They are not parent-children relationship.

Is there any method I can used to get the window first then get the black frame?

I heard that SwingUtilities.getWindowAncestor might be able to do something I want. I tried and did not succeed.

Joey
  • 2,732
  • 11
  • 43
  • 63
  • 1
    Should you really have multiple JFrames at all? http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice – Sinkingpoint Jan 30 '14 at 22:21
  • 1
    If you need to change the parent you will need to supply some kind of reference to the parent, either directly (which I would discourage) or via some kind of model/controller which you can ask to apply the changes on your behalf in some way – MadProgrammer Jan 30 '14 at 22:22
  • I second @MadProgrammer's recs. Of course the devil's all in the details, meaning the details of the solution will depend exquisitely on the details of your code and your problem. I am just a little concerned when you state "parent", because inheritance has nothing to do with this problem. – Hovercraft Full Of Eels Jan 30 '14 at 22:41
  • You can get an array of all "ownerless windows" created by your application by calling `Window.getOwnerlessWindows()` on one of the windows of the application, but again, I have to reiterate what @MadProgrammer states, that you're far better off changing the state of a model, and then having each "view" respond to the change according to its own behaviors. – Hovercraft Full Of Eels Jan 30 '14 at 23:45
  • *"They are not parent-children relationship."* Why? A child of a particular parent does not have to be modal, but there are other advantages of linking them.. – Andrew Thompson Jan 31 '14 at 01:53

1 Answers1

0

You can shoot at this in many different ways. Depending if you need just a one time solution or a dynamic one, the strategy therefor changes. As a one time solution if you just always want to find the main frame, you could either use a reference as already suggested by others or make a singleton main frame, where you just access it like MainFrame.getInstance().doWhateveryouwanted();

However it is not clear which target you are up to, I throw some helpful methods in:

    Frame[] frames = JFrame.getFrames();
    for (Frame frame : frames) {
        if (frame.isActive()) {
            return frame;
        }
    }
    for (Frame frame : frames) {
        if (frame.isVisible()) {
            return frame;
        }
    }

Based on the SwingUtilities class, there are other very helpful methods, look here. Check out SwingUtilities.getDeepestComponent() or SwingUtilities.getAccessibleAt()

gantners
  • 471
  • 4
  • 16