• 追加された行はこの色です。
  • 削除された行はこの色です。
---
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
---
* 概要 [#summary]
JDesktopPaneに配置されたすべてのJInternalFrameの表示状態やアイコン化状態をまとめて切り替えます。
`JDesktopPane`に配置されたすべての`JInternalFrame`の表示状態やアイコン化状態をまとめて切り替えます。

#download(https://drive.google.com/uc?id=13-wqnwhRK26GTmScNQc7AIVsA0F2zMeB)

* サンプルコード [#sourcecode]
#code(link){{
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);
    // reverseList(frames).forEach(m::openFrame);
    frames.forEach(m::deiconifyFrame);
  }
});

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);
  // }
});
}}

* 解説 [#explanation]
- `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(...)`などでは復元できない

* 参考リンク [#reference]
- [[JDesktopPane内のJInternalFrameをJTabbedPaneのタブと入れ替える>Swing/SwapInternalFramesWithTabs]]

* コメント [#comment]
#comment
#comment