• category: swing folder: CloseInternalFrame title: JInternalFrameを閉じる tags: [JInternalFrame, JDesktopPane, DesktopManager] author: aterai pubdate: 2008-05-05T20:51:51+09:00 description: 選択中のJInternalFrameをDesktopManagerなどを使用して外部から閉じる(JDesktopPaneから除去する)方法をテストします。 image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTJcTXtdNI/AAAAAAAAAUY/zL_wkJJa_Ks/s800/CloseInternalFrame.png

概要

選択中のJInternalFrameDesktopManagerなどを使用して外部から閉じる(JDesktopPaneから除去する)方法をテストします。

サンプルコード

closeSelectedFrameAction1 = new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    JInternalFrame f = desktop.getSelectedFrame();
    if (f != null) {
      desktop.getDesktopManager().closeFrame(f);
    }
  }
};

closeSelectedFrameAction2 = new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    JInternalFrame f = desktop.getSelectedFrame();
    if (f != null) {
      f.doDefaultCloseAction();
    }
  }
};

closeSelectedFrameAction3 = new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    try {
      JInternalFrame f = desktop.getSelectedFrame();
      if (f != null) {
        f.setClosed(true);
      }
    } catch (PropertyVetoException ex) {
      ex.printStackTrace();
    }
  }
};

disposeSelectedFrameAction = new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    JInternalFrame f = desktop.getSelectedFrame();
    if (f != null) {
      f.dispose();
    }
  }
};
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、選択されているJInternalFrameをツールバーのボタンやEscキー(OSWindowsの場合のデフォルトは、Ctrl+F4)で閉じることができます。

  • RED
    • JInternalFrame#dispose()メソッドを使用
    • 閉じた後、他のフレームに選択状態が移動しない
  • GREEN
    • DesktopManager#closeFrame(JInternalFrame)メソッドを使用
  • BLUE
    • JInternalFrame#doDefaultCloseAction()メソッドを使用
  • YELLOW
    • JInternalFrame#setClosed(true)メソッドを使用

  • 注: JDK 1.5 + WindowsLookAndFeelでは、JInternalFrameを閉じたときアイコン化されているJInternalFrameには選択状態は移動しない

参考リンク

コメント