How to detect if one swing window is above another one

蹲街弑〆低调 提交于 2019-12-07 09:01:26

问题


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.


回答1:


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 :-)




回答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




回答3:


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



来源:https://stackoverflow.com/questions/17879308/how-to-detect-if-one-swing-window-is-above-another-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!