• title: JRootPaneにリサイズのための装飾を設定する tags: [JFrame, JRootPane, JLayeredPane, JInternalFrame, MetalLookAndFeel] author: aterai pubdate: 2013-06-17T02:37:17+09:00 description: JFrame自体の装飾を削除し、JRootPaneにリサイズのためのウィンドウ装飾(透明)を設定します。

概要

JFrame自体の装飾を削除し、JRootPaneにリサイズのためのウィンドウ装飾(透明)を設定します。

サンプルコード

JFrame frame = new JFrame();
try {
  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
  e.printStackTrace();
}
//XXX: JFrame frame = new JFrame();
frame.setUndecorated(true);

JRootPane root = frame.getRootPane();
root.setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
JLayeredPane layeredPane = root.getLayeredPane();
Component c = layeredPane.getComponent(1);
if (c instanceof JComponent) {
  JComponent orgTitlePane = (JComponent) c;
  orgTitlePane.setVisible(false);
  //layeredPane.remove(orgTitlePane);
}
//JComponent dummyTitlePane = new JLabel();
//layeredPane.add(dummyTitlePane, JLayeredPane.FRAME_CONTENT_LAYER);
//dummyTitlePane.setVisible(true);

JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
p.setBackground(new Color(1f, 1f, 1f, .01f));

p.add(internalFrame);
frame.getContentPane().add(p);
View in GitHub: Java, Kotlin

解説

  • JFrameの装飾を削除
    • JFrame#setUndecorated(true);
  • JInternalFrameContentPaneに追加
  • JRootPaneに装飾を追加、変更
    • JRootPane#setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);で装飾を追加し、リサイズのためのMouseMotionListenerなどを利用
    • JLayeredPaneからタイトルバーを削除
      • 上辺でリサイズできない
    • マウスでリサイズ可能な領域を作成するために、ContentPaneにほぼ透明なBorderをもつJPanelを追加

  • MetalLookAndFeelのみLookAndFeel#getSupportsWindowDecorations()true
    • LookAndFeelを変更する場合は、ContentPane以下から更新することで、JRootPane#setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);が無効にならないようにする必要がある
for (Window window: Frame.getWindows()) {
  if (window instanceof RootPaneContainer) {
    RootPaneContainer rpc = (RootPaneContainer) window;
    SwingUtilities.updateComponentTreeUI(rpc.getContentPane());
  }
}

参考リンク

コメント