TITLE:DesktopManagerでアイコンの再配置
#navi(../)
#tags(DesktopManager, JDesktopPane, JInternalFrame)
RIGHT:Posted by &author(aterai); at 2007-01-15
*DesktopManagerでアイコンの再配置 [#pe96a24f]
``JDesktopPane``のサイズが変更されたとき、アイコン化している``JInternalFrame``の再配置を行います。[http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4765256 Bug ID: 4765256 REGRESSION: Icons in JDesktopPane not repositioned when pane is resized]からソースコードの大部分を引用しています。

-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTRm01W30I/AAAAAAAAAhc/eBhL-DDKkSo/s800/RelocatedIcon.png)

**サンプルコード [#v3086042]
#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);
    }
  }
}
}}

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

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

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

**参考リンク [#j6e9b199]
- [http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4765256 Bug ID: 4765256 REGRESSION: Icons in JDesktopPane not repositioned when pane is resized]
-- [http://forums.sun.com/thread.jspa?threadID=5122847 Swing - JInternalFrame - iconify in a JDesktopPane]
- [http://bugs.sun.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]]

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

#comment