• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*WindowAncestor(親ウィンドウ)の取得 [#i3faa33c]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2005-05-09~
更新日:&lastmod;

#contents
**概要 [#a8e4cc0a]
SwingUtilities.getWindowAncestor()などで、親ウィンドウを取得します。

http://terai.xrea.jp/swing/windowancestor/screenshot.png

**サンプルコード [#vee7c80a]
 JButton button = new JButton(new AbstractAction("フレームのタイトルを表示") {
   public void actionPerformed(ActionEvent ae) {
     final JButton btn  = (JButton)ae.getSource();
     final JFrame frame = (JFrame)SwingUtilities.getWindowAncestor(btn);
     //final JFrame frame = (JFrame)JOptionPane.getFrameForComponent(btn);
     SwingUtilities.invokeLater(new Runnable() {
       public void run() {
         JOptionPane.showMessageDialog(frame,
                     "親フレームのタイトル: "+frame.getTitle(),
                     "タイトル", JOptionPane.INFORMATION_MESSAGE);
       }
     });
   }
 });

-[[サンプルを起動>http://terai.xrea.jp/swing/windowancestor/sample.jnlp]]
-[[jarファイル>http://terai.xrea.jp/swing/windowancestor/sample.jar]]
-[[ソース>http://terai.xrea.jp/swing/windowancestor/src.zip]]
**解説 [#dec5efc1]
自分(コンポーネント)の最初の上位ウィンドウ(親ウィンドウ)を取得します。

SwingUtilities.getWindowAncestor()を使うとjava.awt.Windowが、JOptionPane.getFrameForComponent()の場合はjava.awt.Frameが返されます。

//**参考リンク
**コメント [#td33ce56]
#comment