• category: swing folder: WindowClosingAction title: JPopupMenuなどからWindowを閉じる tags: [JFrame, JPopupMenu, JToolBar, JMenuBar] author: aterai pubdate: 2013-03-11T17:04:49+09:00 description: JPopupMenuや、JToolBarなどに親Windowを閉じるためのActionを作成します。 image: https://lh6.googleusercontent.com/-xWsEbhvjfDY/UT2P-83x0FI/AAAAAAAABmc/7isd5KoGMQc/s800/WindowClosingAction.png

概要

概要

JPopupMenuや、JToolBarなどに親Windowを閉じるためのActionを作成します。

サンプルコード

サンプルコード

#spandel
private static class ExitAction extends AbstractAction{
#spanend
#spanadd
private static class ExitAction extends AbstractAction {
#spanend
  public ExitAction() {
    super("Exit");
  }
#spanadd

#spanend
  @Override public void actionPerformed(ActionEvent e) {
    JComponent c = (JComponent)e.getSource();
    JComponent c = (JComponent) e.getSource();
    Window window = null;
    Container parent = c.getParent();
    if(parent instanceof JPopupMenu) {
      JPopupMenu popup = (JPopupMenu)parent;
      JComponent invoker = (JComponent)popup.getInvoker();
    if (parent instanceof JPopupMenu) {
      JPopupMenu popup = (JPopupMenu) parent;
      JComponent invoker = (JComponent) popup.getInvoker();
      window = SwingUtilities.getWindowAncestor(invoker);
    }else if(parent instanceof JToolBar) {
      JToolBar toolbar = (JToolBar)parent;
      if(((BasicToolBarUI)toolbar.getUI()).isFloating()) {
    } else if (parent instanceof JToolBar) {
      JToolBar toolbar = (JToolBar) parent;
      if (((BasicToolBarUI) toolbar.getUI()).isFloating()) {
        window = SwingUtilities.getWindowAncestor(toolbar).getOwner();
      }else{
      } else {
        window = SwingUtilities.getWindowAncestor(toolbar);
      }
    }else{
    } else {
      Component invoker = c.getParent();
      window = SwingUtilities.getWindowAncestor(invoker);
    }
    if(window!=null) {
      //window.dispose();
    if (window != null) {
      // window.dispose();
      window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
    }
  }
}
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、親となるJFrameを取得し、window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); 使って、終了イベントを実行しています。

解説

上記のサンプルでは、親となるJFrameを取得してwindow.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));を使用し、これを閉じるためのイベントを実行しています。 コンポーネントの親Windowを取得する場合、SwingUtilities.getWindowAncestor(...)などが使用可能ですが、HeavyWeightWindowJPopupMenuFloating中のJToolBarでは親Windowとは異なるWindowが使用されるので注意が必要です。
  • JPopupMenu
    • JPopupMenu#getInvoker()を使って、JComponent#setComponentPopupMenu(popup)で設定したコンポーネントを取得し、SwingUtilities.getWindowAncestor(...)で、親Windowを取得
    • JPopupMenu#getInvoker()を使用してJComponent#setComponentPopupMenu(popup)で設定したコンポーネントを取得し、SwingUtilities.getWindowAncestor(...)メソッドで親Windowを取得
  • JMenuBar
    • SwingUtilities.getWindowAncestor(...)で、自身の親Windowを取得
    • SwingUtilities.getWindowAncestor(...)メソッドで自身の親Windowを取得
  • JToolBar
    • 移動中の場合、JComponent#setComponentPopupMenu(toolbar)で取得した移動中のWindowの親WindowWindow#getOwner()で取得
    • 移動中では無い場合、SwingUtilities.getWindowAncestor(toolbar)で、自身の親Windowを取得
    • 移動中の場合、JComponent#setComponentPopupMenu(toolbar)メソッドで取得した移動中のWindowの親WindowWindow#getOwner()で取得
    • 移動中では無い場合、SwingUtilities.getWindowAncestor(toolbar)メソッドで自身の親Windowを取得

参考リンク

参考リンク

コメント

コメント