问题
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
use
CardLayoutinstead of bunch ofJFrames, without any popup windowsuse
CardLayoutinstead of bunch ofJFramesand for another popup window to useJDialog()/JWindow(JTextComponentsaren't editable)use
CardLayoutinstead of bunch ofJFramesand for another popup window to useJDialog()/JWindow(JTextComponentsaren't editable) withCardLayoutfor multiple switching betweens view in popup container, note usageXxx.pack()is required for proper sizing after card is switchedset
JFrameas parent forJDialog()/JWindow(), then parent v.s. child are question ofisVisible,isDisplayable, etc, and rest events implemented inWindowEvent
回答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