4

I have several unrelated JFrames opened in my app. And on app exit I should remember the preferences of all of them to restore them the same view they was before exit.

The problem is when these frames are located one above another, they should be restored in the same order. Another way the smaller frames would not be visible under bigger ones.

QUESTION: How can I detect the deep location each of frames in this case?

UPD: I detect this case by comparing the frames' location/size pairs, but I still do not how to resolve my issue.

UPD2: Note: the several jframes ui is not default state. One JFrame shown by default, but user can use several if he want. My question is not about how to make single JFrame, but how to operate with several ones.

user229044
  • 232,980
  • 40
  • 330
  • 338
SeniorJD
  • 6,946
  • 4
  • 36
  • 53
  • 4
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Jul 26 '13 at 10:51
  • 3
    *"I can't find the my question' answer on your link.."* That is why it is a **comment**, not an **answer.** But -1 for your (apparent) intention to foist such a monstrous GUI on your innocent users (both of them). – Andrew Thompson Jul 26 '13 at 11:05
  • 1
    @SeniorJD : You can simply use [__Window.isFocusableWindow()__](http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#isFocusableWindow()), to check as to which `Jframe` is at top. I hope this can work :-) – nIcE cOw Jul 26 '13 at 11:19
  • I don't believe this is possible without restoring to JNI/JNA – MadProgrammer Jul 26 '13 at 11:21
  • It would not be overly reliable either. On X11 some window managers refuse to cooperate when an application tries to micromanage the windows. – kiheru Jul 26 '13 at 11:48
  • @AndrewThompson hard day? :) – SeniorJD Jul 26 '13 at 12:03
  • @nIcEcOw please write your comment as an answer, it helps me with a gr8 idea! – SeniorJD Jul 26 '13 at 12:35

3 Answers3

3

Simply use Window.isFocusableWindow(), to check as to which JFrame lies on top of all others.

Though I will also insist on not using more than a single JFrame, for any application :-)

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • 1
    It is close to my real solution: I've created the global `FocusListener` and added it for every of my JFrame. When the window is under focus, it is added to a list (or setting the last index, if it is already there), then, using this list, I detect the `Z-Order` of windows. Thanks! – SeniorJD Jul 26 '13 at 13:09
  • *for any application :-)* What about File Explorer? Don't you use several of its Windows at the time? – SeniorJD Jul 26 '13 at 13:13
  • 1
    @SeniorJD : Never worked on such things before, but what ever I have experienced so far, never felt a need of more than one `JFrame`. I use to play games online on [Game Ranger](http://www.gameranger.com/), and their multiple windows always use to annoy me :-). JDialog can be chosen instead of a `JFrame`, as that will maintain some relation of `parent-child`, which can help in various situations. – nIcE cOw Jul 26 '13 at 13:23
  • See the UPD2 please. The several JFrames is just `if-you-want` feature, but not `must have`. – SeniorJD Jul 26 '13 at 13:33
2

QUESTION: How can I detect the deep location each of frames in this case?

UPD: I detect this case by comparing the frames' location/size pairs, but I still do not how to resolve my issue.

  • you can to loop inside arrays of Top-Level Containers

.

  Window[] wins = Window.getWindows();
  for (int i = 0; i < wins.length; i++) {
        if (wins[i] instanceof JFrame) {
            wins[i].wheteverMethodImplementedInConcreteAPI;
        } //else if ... JDialog, JWindow, e.i. AWT Containers
  }

.

Hmm.. I can't find the my question' answer on your link actually...Except the bad bad practise to use several JFrames :)

.

  • see Remove Top-Level Container on Runtime, then there are

    1. use CardLayout instead of bunch of JFrames, without any popup windows

    2. use CardLayout instead of bunch of JFrames and for another popup window to use JDialog()/JWindow(JTextComponents aren't editable)

    3. use CardLayout instead of bunch of JFrames and for another popup window to use JDialog()/JWindow(JTextComponents aren't editable) with CardLayout for multiple switching betweens view in popup container, note usage Xxx.pack() is required for proper sizing after card is switched

    4. set JFrame as parent for JDialog()/JWindow(), then parent v.s. child are question of isVisible, isDisplayable, etc, and rest events implemented in WindowEvent

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • `you can to loop inside arrays of Top-Level Containers` Yes, I can. And what would I be able to get there? – SeniorJD Jul 26 '13 at 11:52
  • And what would I be able to get there? == Window[] wins = Window.getWindows(); please to see my question edited, really forgot add this code line – mKorbel Jul 26 '13 at 12:12
  • Yes, I mean what info about their `Z-order` can I get from this `array`? And how? – SeniorJD Jul 26 '13 at 12:15
  • wihtout chainging parent v.s. childs there aren't ordering of JFrames, in (AFAIK in another PL too) you need to hold this index, increasing, maybe in protected methods, but comment by @Andrew Thompson is about good practicies in all PL (created on_screen GUI) – mKorbel Jul 26 '13 at 12:22
0

You can use the getComponentZOrder() to get the z-index of the frames and set it using setComponentZOrder().

rahulserver
  • 10,411
  • 24
  • 90
  • 164
  • From the which object should I use the method? The `JFrames` are `unrelated`, they do not have the same `parent` or the link to each other. – SeniorJD Jul 26 '13 at 10:57
  • 1
    I don't think this can be applied to frames – MadProgrammer Jul 26 '13 at 11:16
  • http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html So JFrame inherits Component – rahulserver Jul 26 '13 at 11:17
  • 5
    `getComponentZOrder` takes a `Component` parameter which is consider a child of the container calling it... *"Returns the z-order index of the component inside the container. The higher a component is in the z-order hierarchy, the lower its index. The component with the lowest z-order index is painted last, above all other child components."* - Or I will accept a working example – MadProgrammer Jul 26 '13 at 11:19
  • 2
    @MadProgrammer *"Or I will accept a working example"* Hear, hear.. Let the code do the talking. – Andrew Thompson Jul 26 '13 at 12:09