Swing/WindowAncestor のバックアップ差分(No.13)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - 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)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:WindowAncestor(親ウィンドウ)の取得 #navi(../) #tags(JFrame, Window) RIGHT:Posted by &author(aterai); at 2005-05-09 * WindowAncestor(親ウィンドウ)の取得 [#i3faa33c] --- title: WindowAncestor(親ウィンドウ)の取得 tags: [JFrame, Window] author: aterai pubdate: 2005-05-09 description: SwingUtilities.getWindowAncestor()などで、親ウィンドウを取得します。 --- * 概要 [#i3faa33c] `SwingUtilities.getWindowAncestor()`などで、親ウィンドウを取得します。 - &jnlp; - &jar; - &zip; #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTWr-a0yaI/AAAAAAAAApo/Wm-nQMxDh4s/s800/WindowAncestor.png) #ref(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTWr-a0yaI/AAAAAAAAApo/Wm-nQMxDh4s/s800/WindowAncestor.png) ** サンプルコード [#vee7c80a] * サンプルコード [#vee7c80a] #code(link){{ 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); } })); }} ** 解説 [#dec5efc1] * 解説 [#dec5efc1] 自分(コンポーネント)の最初の上位ウィンドウ(親ウィンドウ)を取得します。 - [http://docs.oracle.com/javase/jp/6/api/javax/swing/SwingUtilities.html#getWindowAncestor(java.awt.Component) SwingUtilities.getWindowAncestor(Component c)] -- `SwingUtilities.windowForComponent(Component c)`は、`getWindowAncestor`をラップしただけのメソッド -- 親の`java.awt.Window`が返る -- 親`Window`が無い場合は、`null` - [http://docs.oracle.com/javase/jp/6/api/javax/swing/SwingUtilities.html#getRoot(java.awt.Component) SwingUtilities.getRoot(Component c)] -- 親の`java.awt.Component(java.awt.Window または java.awt.Applet)`が返る -- 親のコンポーネント(`java.awt.Window`または`java.awt.Applet`)が返る --- `Window`の場合は、`c.getParent()`で見つかる最初の上位`Window`オブジェクトだが、`Applet`の場合は、`JComponent#getTopLevelAncestor()`とは異なり、最後の上位`Applet`オブジェクト -- どちらも存在しない場合は、`null` - [http://docs.oracle.com/javase/jp/6/api/javax/swing/JComponent.html#getTopLevelAncestor() JComponent#getTopLevelAncestor()] -- 自身の親コンテナ(`java.awt.Window`または`java.awt.Applet`)`が返る -- 自身の親コンテナ(`java.awt.Window`または`java.awt.Applet`)が返る -- 親コンテナが無い場合は、`null` -- 下のコメント参照 - [http://docs.oracle.com/javase/jp/6/api/javax/swing/JOptionPane.html#getFrameForComponent(java.awt.Component) JOptionPane.getFrameForComponent(Component parentComponent)] -- 親の`java.awt.Frame`が返る -- 有効な親`Frame`が無い場合は`JOptionPane.getRootFrame()`で、非表示にしている`TookKit Private`なフレームが返る -- `JOptionPane`用? ** 参考リンク [#q80fd7f2] * 参考リンク [#q80fd7f2] - [http://docs.oracle.com/javase/jp/6/api/javax/swing/SwingUtilities.html#getRoot(java.awt.Component) SwingUtilities.getRoot(Component c)] を追加 -- via: [http://stackoverflow.com/questions/9650874/java-swing-obtain-window-jframe-from-inside-a-jpanel Java/Swing: Obtain Window/JFrame from inside a JPanel - Stack Overflow] ** コメント [#td33ce56] - `JComponent#getTopLevelAncestor()`でもほぼ同じ内容が取得できるような感じですね(自分自身からスタートするか、親からスタートするかの違いはあるようですが)。ただ、この場合、`Window`の他に`Applet`が戻される場合もあるようですが。 -- [[syo]] &new{2006-08-24 (木) 17:07:25}; -- 補足ありがとうございます。`Window`か`Applet`か気にする必要が無いのは便利そうですね。 -- [[aterai]] &new{2006-08-25 (金) 12:46:43}; * コメント [#td33ce56] #comment - `JComponent#getTopLevelAncestor()`でもほぼ同じ内容が取得できるような感じですね(自分自身からスタートするか、親からスタートするかの違いはあるようですが)。ただ、この場合、`Window`の他に`Applet`が戻される場合もあるようですが。 -- &user(syo); &new{2006-08-24 (木) 17:07:25}; -- 補足ありがとうございます。`Window`か`Applet`か気にする必要が無いのは便利そうですね。 -- &user(aterai); &new{2006-08-25 (金) 12:46:43}; #comment