• category: swing folder: RelocatedIcon title: DesktopManagerでアイコンの再配置 tags: [DesktopManager, JDesktopPane, JInternalFrame] author: aterai pubdate: 2007-01-15T12:27:58+09:00 description: JDesktopPaneのサイズが変更されたとき、アイコン化しているJInternalFrameの再配置を行います。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTRm01W30I/AAAAAAAAAhc/eBhL-DDKkSo/s800/RelocatedIcon.png

概要

JDesktopPaneのサイズが変更されたとき、アイコン化しているJInternalFrameの再配置を行います。Bug ID: 4765256 REGRESSION: Icons in JDesktopPane not repositioned when pane is resizedからソースコードの大部分を引用しています。

サンプルコード

class ReIconifyDesktopManager extends DefaultDesktopManager {
  public void reIconifyFrame(JInternalFrame jif) {
    deiconifyFrame(jif);
    Rectangle r = getBoundsForIconOf(jif);
    iconifyFrame(jif);
    jif.getDesktopIcon().setBounds(r);
  }
}
View in GitHub: Java, Kotlin
private void doReIconify(JDesktopPane desktopPane) {
  DesktopManager dm = desktopPane.getDesktopManager();
  if (dm instanceof ReIconifyDesktopManager) {
    ReIconifyDesktopManager rdm = (ReIconifyDesktopManager) dm;
    for (JInternalFrame f: desktopPane.getAllFrames()) {
      if (f.isIcon()) {
        rdm.reIconifyFrame(f);
      }
    }
  }
}

解説

上記のサンプルでは、JDesktopPaneがリサイズされた場合、以下のような手順で再配置を行っています。

  1. アイコン化したJInternalFrameを一旦、元のサイズと位置に復元
  2. アイコン化した場合の位置を再計算
  3. 再びアイコン化
  4. 再計算した位置への移動

GTKLookAndFeelの場合、アイコンを移動することは出来ないので、このサンプルには意味がありません。

参考リンク

コメント