• category: swing folder: DesktopIconSize title: JInternalFrameをアイコン化した場合のサイズを変更する tags: [JInternalFrame, JDesktopPane, LookAndFeel] author: aterai pubdate: 2016-02-15T00:00:57+09:00 description: JInternalFrameをアイコン化したときに使用されるJDesktopIconのサイズを変更します。 image: https://lh3.googleusercontent.com/-TEU2w7fUDtA/VsCIqEiNtoI/AAAAAAAAONk/VP5SO5nYnuw/s800-Ic42/DesktopIconSize.png

概要

JInternalFrameをアイコン化したときに使用されるJDesktopIconのサイズを変更します。

サンプルコード

UIManager.put("DesktopIcon.width", 150);
//...
JInternalFrame f = new JInternalFrame(t, true, true, true, true);
f.setDesktopIcon(new JInternalFrame.JDesktopIcon(f) {
  @Override public Dimension getPreferredSize() {
    return new Dimension(150, 40);
  }
});
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、JInternalFrameをアイコン化したときに使用されるJDesktopIconのサイズ変更をLookAndFeel毎にテストしています。

  • UIManager.put("DesktopIcon.width", DESKTOPICON_WIDTH);
    • MetalLookAndFeelWindowsLookAndFeelなどで、JDesktopIconの幅を指定可能
    • NimbusLookAndFeelMotifLookAndFeelでは効果がない
  • Override: JInternalFrame.JDesktopIcon#getPreferredSize()
    • JInternalFrame.JDesktopIcon#getPreferredSize()メソッドをオーバーライドして、LookAndFeelに依存せずにサイズを変更
    • MotifLookAndFeelの場合、タイトルバー状ではなくアイコン状なので、new Dimension(64, 64 + 32)を使用
  • メモ:
    • デフォルト状態のNimbusLookAndFeelで、JDesktopIconの高さがJInternalFrameによって変化する?
    • DefaultDesktopManager#getBoundsForIconOf(...)メソッドをオーバーライドしてサイズ変更することも可能だが、アイコンの位置を計算し直す必要がある
desktop.setDesktopManager(new DefaultDesktopManager() {
  @Override protected Rectangle getBoundsForIconOf(JInternalFrame f) {
    Rectangle r = super.getBoundsForIconOf(f);
    r.width = 200;
    return r;
  }
});

参考リンク

コメント