---
category: swing
folder: RelocatedIcon
title: DesktopManagerでアイコンの再配置
tags: [DesktopManager, JDesktopPane, JInternalFrame]
author: aterai
pubdate: 2007-01-15
description: JDesktopPaneのサイズが変更されたとき、アイコン化しているJInternalFrameの再配置を行います。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTRm01W30I/AAAAAAAAAhc/eBhL-DDKkSo/s800/RelocatedIcon.png
---
* 概要 [#summary]
`JDesktopPane`のサイズが変更されたとき、アイコン化している`JInternalFrame`の再配置を行います。[http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4765256 Bug ID: 4765256 REGRESSION: Icons in JDesktopPane not repositioned when pane is resized]からソースコードの大部分を引用しています。

#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTRm01W30I/AAAAAAAAAhc/eBhL-DDKkSo/s800/RelocatedIcon.png)

* サンプルコード [#sourcecode]
#code(link){{
class ReIconifyDesktopManager extends DefaultDesktopManager {
  public void reIconifyFrame(JInternalFrame jif) {
    deiconifyFrame(jif);
    Rectangle r = getBoundsForIconOf(jif);
    iconifyFrame(jif);
    jif.getDesktopIcon().setBounds(r);
  }
}
}}
#code{{
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);
      }
    }
  }
}
}}

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

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

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

* 参考リンク [#reference]
- [http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4765256 Bug ID: 4765256 REGRESSION: Icons in JDesktopPane not repositioned when pane is resized]
-- via: [https://community.oracle.com/thread/1374482 Swing - JInternalFrame - iconify in a JDesktopPane]
- [http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4110799 Bug ID: 4110799 JInternalFrame icon position unchanged w/ resize]
-- [http://d.hatena.ne.jp/tori31001/20060901 JInternalFrameは最初にアイコン化しておかないと位置が更新されない]
-- [[JInternalFrameを一番手前に表示>Swing/LayeredPane]]

* コメント [#comment]
#comment
- `1.7.0 b38`で修正されているようです。[http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6647340 Bug ID: 6647340 Minimized JInternalFrame icons appear in incorrect positions if the main frame is resized] -- &user(aterai); &new{2008-12-10 (水) 21:27:26};
- `Windows7` + `WindowsLookAndFeel`で`JDesktopPane`の背景が黒になる: [http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7012008 Bug ID: JDK-7012008 JDesktopPane - Wrong background color with Win7+WindowsLnf] -- &user(aterai); &new{2011-10-04 (火) 16:58:32};
//[https://bugs.openjdk.java.net/browse/JDK-7012008 JDK-7012008 JDesktopPane - Wrong background color with Win7+WindowsLnf - Java Bug System]
//[http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7008416 Bug ID: 7008416 JDesktopPane - Wrong background color with Win7+WindowsLnf] -- &user(aterai); &new{2011-10-04 (火) 16:58:32};

#comment