TITLE:WindowAncestor(親ウィンドウ)の取得

WindowAncestor(親ウィンドウ)の取得

編集者:Terai Atsuhiro~

作成日:2005-05-09
更新日:2023-08-25 (金) 13:56:29
  • category: swing folder: WindowAncestor title: WindowAncestor(親ウィンドウ)の取得 tags: [JFrame, Window] author: aterai pubdate: 2005-05-09T21:28:35+09:00 description: SwingUtilities.getWindowAncestor()などで、親ウィンドウを取得します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTWr-a0yaI/AAAAAAAAApo/Wm-nQMxDh4s/s800/WindowAncestor.png

概要

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

概要

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

#screenshot

サンプルコード

#spanend
#spandel
JButton button = new JButton(new AbstractAction("フレームのタイトルを表示") {
#spanend
  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);
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        JOptionPane.showMessageDialog(frame,
                    "親フレームのタイトル: "+frame.getTitle(),
                    "タイトル", JOptionPane.INFORMATION_MESSAGE);
      }
    });
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
#spanadd
JButton button = new JButton("show frame title");
#spanend
#spanadd
button.addActionListener(e -> {
#spanend
  // 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);
  }
});
  • &jnlp;
  • &jar;
  • &zip;

解説

解説

自分(コンポーネント)の最初の上位ウィンドウ(親ウィンドウ)を取得します。
SwingUtilities.getWindowAncestor、SwingUtilities.getRootメソッドを使うとjava.awt.Windowが、JOptionPane.getFrameForComponentメソッドを使うとjava.awt.Frameが返されます。
  • SwingUtilities.getWindowAncestor(Component c)
    • SwingUtilities.windowForComponent(Component c)は、このgetWindowAncestorをラップしただけのメソッド
    • 親のjava.awt.Windowが返る
    • Windowが無い場合はnullが返る
    • 引数のComponent自体がWindowの場合そのWindowのオーナーWindowが返る
      • オーナーWindownullの場合は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から呼ばれた場合はそのまま自身が返る
    • 下のコメント参照

コメント

  • JComponent#getTopLevelAncestor()でもほぼ同じ内容が取得できるような感じですね(自分自身からスタートするか、親からスタートするかの違いはあるようですが)。ただ、この場合、Windowの他にAppletが戻される場合もあるようですが。 -- syo
  • 補足ありがとうございます。WindowかAppletか気にする必要が無いのは便利そうですね。 -- terai
  • JOptionPane.getFrameForComponent(Component parentComponent)
    • 親のjava.awt.Frameが返る
    • 有効な親Frameが無い場合はJOptionPane.getRootFrame()で非表示にしているTookKit Privateなフレームが返る
    • JOptionPane用?

参考リンク

コメント