• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:DesktopManagerでアイコンの再配置
#navi(../)
*DesktopManagerでアイコンの再配置 [#pe96a24f]
Posted by [[terai]] at 2007-01-15
---
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
---
* 概要 [#summary]
`JDesktopPane`のサイズが変更されたとき、アイコン化している`JInternalFrame`の再配置を行います。[https://bugs.openjdk.org/browse/JDK-4765256 [JDK-4765256] REGRESSION: Icons in JDesktopPane not repositioned when pane is resized - Java Bug System]からソースコードの大部分を引用しています。

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

**概要 [#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]]からソースコードの大部分を引用しています。

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

#screenshot

**サンプルコード [#v3086042]
#code{{
* サンプルコード [#sourcecode]
#code(link){{
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]);

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 を一旦、元のサイズと位置に復元
+アイコン化した場合の位置を再計算
+再びアイコン化
+再計算した位置への移動
* 解説 [#explanation]
- `JDesktopPane`がリサイズされた場合、以下のような手順で再配置を実行
-- `GTKLookAndFeel`を使用する場合、アイコンの移動自体がデフォルトでは不可なのでこの設定は無意味になる

**参考リンク [#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`を一旦元のサイズと位置に復元
+ アイコン化した場合の位置を再計算
+ 再びアイコン化
+ 再計算した位置への移動

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

**コメント [#u2043794]
* コメント [#comment]
#comment
- [https://bugs.openjdk.org/browse/JDK-6647340 &#91;JDK-6647340&#93; Minimized JInternalFrame icons appear in incorrect positions if the main frame is resized - Java Bug System]は、`1.7.0 b38`で修正済み。 -- &user(aterai); &new{2008-12-10 (水) 21:27:26};
- `Windows7` + `WindowsLookAndFeel`で`JDesktopPane`の背景が黒になる: [https://bugs.openjdk.org/browse/JDK-7012008 Bug ID: JDK-7012008 JDesktopPane - Wrong background color with Win7+WindowsLnf] -- &user(aterai); &new{2011-10-04 (火) 16:58:32};
// [https://bugs.openjdk.org/browse/JDK-7012008 JDK-7012008 JDesktopPane - Wrong background color with Win7+WindowsLnf - Java Bug System]
// [https://bugs.openjdk.org/browse/JDK-7008416 Bug ID: 7008416 JDesktopPane - Wrong background color with Win7+WindowsLnf] -- &user(aterai); &new{2011-10-04 (火) 16:58:32};

#comment