• category: swing folder: ShowDesktop title: JDesktopPaneに配置されたすべてのJInternalFrameの表示状態を切り替える tags: [JDesktopPane, JInternalFrame] author: aterai pubdate: 2021-08-02T05:10:35+09:00 description: JDesktopPaneに配置されたすべてのJInternalFrameの表示状態やアイコン化状態をまとめて切り替えます。 image: https://drive.google.com/uc?id=13-wqnwhRK26GTmScNQc7AIVsA0F2zMeB

概要

JDesktopPaneに配置されたすべてのJInternalFrameの表示状態やアイコン化状態をまとめて切り替えます。

サンプルコード

JDesktopPane desktop = new JDesktopPane();

JToggleButton button1 = new JToggleButton("Iconify Frames");
button1.addActionListener(e -> {
  DesktopManager m = desktop.getDesktopManager();
  List<JInternalFrame> frames = Arrays.asList(desktop.getAllFrames());
  if (((AbstractButton) e.getSource()).isSelected()) {
    reverseList(frames).forEach(m::iconifyFrame);
  } else {
    reverseList(frames).forEach(m::openFrame);
  }
});

JToggleButton button2 = new JToggleButton("Show Desktop");
button2.addActionListener(e -> {
  boolean show = ((AbstractButton) e.getSource()).isSelected();
  JInternalFrame[] frames = desktop.getAllFrames();
  // TEST: Arrays.asList(frames).forEach(f -> f.setVisible(!show));
  reverseList(Arrays.asList(frames)).forEach(f -> f.setVisible(!show));
  // for (int i = frames.length - 1; i >= 0; i--) {
  //   frames[i].setVisible(!show);
  // }
});
View in GitHub: Java, Kotlin

解説

  • Iconify Frames
    • JDesktopPane#getAllFrames()ですべてのJInternalFrameを取得し、DesktopManager#iconifyFrame(...)でアイコン化、DesktopManager#openFrame(...)で再オープン
    • JDesktopPane#getAllFrames()で取得した配列は手前から奥の順番に並んでいるので、アイコン化、再オープンする際には逆順で実行しないとこの並びが反転してしまう
    • DesktopManager#openFrame(...)ではなくDesktopManager#deiconifyFrame()を使用すると復元の際に反転の必要はない
  • Show Desktop
    • 同じくJDesktopPane#getAllFrames()ですべてのJInternalFrameを取得し、JInternalFrame#setVisible(false)で非表示化、JInternalFrame#setVisible(true)で再表示
    • JDesktopPane#getAllFrames()で取得した配列は手前から奥の順番に並んでいるので、非表示化、再表示する際には逆順で実行しないとこの並びが反転してしまう
    • DesktopManager#closeFrame(...)でも非表示化が可能だが、JDesktopPaneから削除されてしまうのでDesktopManager#openFrame(...)などでは復元できない

参考リンク

コメント