Summary
SwingUtilities.getWindowAncestor()などで、親ウィンドウを取得します。
Screenshot

Advertisement
Source Code Examples
JButton button = new JButton("show frame title");
button.addActionListener(e -> {
// Container w = ((JComponent) e.getSource()).getTopLevelAncestor();
Window w = SwingUtilities.getWindowAncestor((Component) e.getSource());
// Frame frame = JOptionPane.getFrameForComponent((Component) e.getSource());
if (w instanceof Frame) {
Frame frame = (Frame) w;
String msg = "parentFrame.getTitle(): " + frame.getTitle();
JOptionPane.showMessageDialog(
frame, msg, "title", JOptionPane.INFORMATION_MESSAGE);
}
});
View in GitHub: Java, KotlinDescription
自分(コンポーネント)の最初の上位ウィンドウ(親ウィンドウ)を取得します。
- SwingUtilities.getWindowAncestor(Component c)
SwingUtilities.windowForComponent(Component c)は、このgetWindowAncestorをラップしただけのメソッド- 親の
java.awt.Windowが返る - 親
Windowが無い場合はnullが返る - 引数の
Component自体がWindowの場合そのWindowのオーナーWindowが返る- オーナー
Windowがnullの場合はnullが返る
- オーナー
- SwingUtilities.getRoot(Component c)
- 親の
Component(java.awt.Windowまたはjava.awt.Applet)が返るWindowの場合はc.getParent()で見つかる最初の上位Windowオブジェクトだが、Appletの場合はJComponent#getTopLevelAncestor()とは異なり最後の上位Appletオブジェクト
- どちらも存在しない場合は
null - 引数の
Component自体がWindowの場合はそのまま自身が返る
- 親の
- JComponent#getTopLevelAncestor()
- 自身の親
Container(java.awt.Windowまたはjava.awt.Applet)が返る - 親
Containerが無い場合はnull java.awt.Windowまたはjava.awt.Appletから呼ばれた場合はそのまま自身が返る- 下のコメント参照
- 自身の親
- JOptionPane.getFrameForComponent(Component parentComponent)
- 親の
java.awt.Frameが返る - 有効な親
Frameが無い場合はJOptionPane.getRootFrame()で非表示にしているTookKit Privateなフレームが返る JOptionPane用?
- 親の