How to get the parent container of a JWindow in Swing?

守給你的承諾、 提交于 2019-12-11 14:37:52

问题


I have one JFrame and calling a JDialog from that frame while initializing. In the dialog have a JButton, when clicking on the button a new JWindow opens.

Can I fix the position of window in the main frame? How to access the parent container (JFrame extended) from JWindow extended class?


回答1:


Use SwingUtilities.getWindowAncestor, if inside the JWindow class, refer it as this, if no, simply put object inside:

JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);

JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(myJWindow);


来源:https://stackoverflow.com/questions/30122731/how-to-get-the-parent-container-of-a-jwindow-in-swing

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