Swing/WindowAncestor のバックアップ(No.11)
- バックアップ一覧
 - 差分 を表示
 - 現在との差分 を表示
 - 現在との差分 - Visual を表示
 - ソース を表示
 - Swing/WindowAncestor へ行く。
  
- 1 (2005-09-02 (金) 21:28:35)
 - 2 (2005-09-30 (金) 16:31:37)
 - 3 (2006-02-27 (月) 16:37:17)
 - 4 (2006-08-24 (木) 17:07:25)
 - 5 (2006-08-25 (金) 12:46:43)
 - 6 (2007-05-04 (金) 03:29:20)
 - 7 (2007-12-11 (火) 14:11:52)
 - 8 (2008-05-09 (金) 14:34:28)
 - 9 (2011-03-20 (日) 16:58:24)
 - 10 (2012-03-13 (火) 20:58:15)
 - 11 (2013-03-30 (土) 21:01:15)
 - 12 (2013-10-15 (火) 17:36:02)
 - 13 (2015-02-09 (月) 17:22:52)
 - 14 (2015-06-05 (金) 17:16:52)
 - 15 (2015-12-22 (火) 15:31:00)
 - 16 (2016-03-29 (火) 18:42:44)
 - 17 (2016-08-18 (木) 12:44:25)
 - 18 (2017-04-04 (火) 14:13:45)
 - 19 (2017-04-07 (金) 13:51:51)
 - 20 (2017-10-06 (金) 14:54:33)
 - 21 (2019-04-10 (水) 13:45:03)
 - 22 (2021-01-20 (水) 18:23:06)
 - 23 (2023-08-25 (金) 13:56:29)
 - 24 (2025-01-03 (金) 08:57:02)
 - 25 (2025-01-03 (金) 09:01:23)
 - 26 (2025-01-03 (金) 09:02:38)
 - 27 (2025-01-03 (金) 09:03:21)
 - 28 (2025-01-03 (金) 09:04:02)
 - 29 (2025-06-19 (木) 12:41:37)
 - 30 (2025-06-19 (木) 12:43:47)
 
 
TITLE:WindowAncestor(親ウィンドウ)の取得
Usage: #tags(tags)Posted by aterai at 2005-05-09
WindowAncestor(親ウィンドウ)の取得
SwingUtilities.getWindowAncestor()などで、親ウィンドウを取得します。
- &jnlp;
 - &jar;
 - &zip;
 
サンプルコード
JButton button = new JButton(new AbstractAction("フレームのタイトルを表示") {
  @Override public void actionPerformed(ActionEvent e) {
    JButton btn  = (JButton)e.getSource();
    JFrame f = (JFrame)SwingUtilities.getWindowAncestor(btn);
    //JFrame f = (JFrame)btn.getTopLevelAncestor();
    //JFrame f = (JFrame)JOptionPane.getFrameForComponent(btn);
    JOptionPane.showMessageDialog(f, "parentFrame.getTitle(): "+f.getTitle(),
                                  "title", JOptionPane.INFORMATION_MESSAGE);
  }
}));
View in GitHub: Java, Kotlin解説
自分(コンポーネント)の最初の上位ウィンドウ(親ウィンドウ)を取得します。
- SwingUtilities.getWindowAncestor(Component c)
- SwingUtilities.windowForComponent(Component c) は、getWindowAncestorをラップしただけのメソッド
 - 親のjava.awt.Window が返る
 - 親Window が無い場合は、null
 
 
- SwingUtilities.getRoot(Component c)
- 親のjava.awt.Component(java.awt.Window または java.awt.Applet)が返る
- Window の場合は、c.getParent() で見つかる最初の上位 Window オブジェクトだが、Applet の場合は、JComponent#getTopLevelAncestor()とは異なり、最後の上位 Applet オブジェクト
 
 - どちらも存在しない場合は、null
 
 - 親のjava.awt.Component(java.awt.Window または java.awt.Applet)が返る
 
- JComponent#getTopLevelAncestor()
- 自身の親コンテナ(java.awt.Window または java.awt.Applet) が返る
 - 親コンテナが無い場合は、null
 - 下のコメント参照
 
 
- JOptionPane.getFrameForComponent(Component parentComponent)
- 親のjava.awt.Frame が返る
 - 有効な親Frameが無い場合は JOptionPane.getRootFrame() で、非表示にしているTookKit Privateなフレームが返る
 - JOptionPane用?
 
 
