TITLE:DesktopManagerでアイコンの再配置
#navi(../)
*DesktopManagerでアイコンの再配置 [#pe96a24f]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2007-01-15~
更新日:&lastmod;

#contents

**概要 [#w9d38ffd]
JDesktopPaneのサイズが変更されたとき、アイコン化しているJInternalFrameの再配置を行います。[[Bug ID: 4765256 REGRESSION: Icons in JDesktopPane not repositioned when pane is resized>http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4765256]]からソースコードの大部分を引用しています。

#screenshot

**サンプルコード [#v3086042]
#code{{
class ReIconifyDesktopManager extends DefaultDesktopManager {
  public void reIconifyFrame(JInternalFrame jif) {
    deiconifyFrame(jif);
    Rectangle r = getBoundsForIconOf(jif);
    iconifyFrame(jif);
    jif.getDesktopIcon().setBounds(r);
  }
}
//......
ReIconifyDesktopManager rdm = new ReIconifyDesktopManager();
desktop.setDesktopManager(rdm);
desktop.addComponentListener(new ComponentAdapter() {
  public void componentResized(ComponentEvent ce) {
    JDesktopPane dp = (JDesktopPane)ce.getComponent();
    ReIconifyDesktopManager dm = (ReIconifyDesktopManager)dp.getDesktopManager();
    JInternalFrame[] jif = dp.getAllFrames();
    for(int i=0;i<jif.length;i++) {
      if(jif[i].isIcon()) dm.reIconifyFrame(jif[i]);
    }
  }
});
//......
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#q4580c19]
上記のサンプルでは、JToggleButtonが選択されている場合、再配置を行っています。

JDesktopPaneがリサイズされた場合、以下のような手順で再配置を行っています。
-アイコン化したJInternalFrame を一旦、元のサイズと位置に復元
-アイコン化した場合の位置を再計算
-再びアイコン化
-再計算した位置への移動

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

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