TITLE:JRootPaneにリサイズのための装飾を設定する

Posted by at 2013-06-17

JRootPaneにリサイズのための装飾を設定する

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

  • &jnlp;
  • &jar;
  • &zip;
WindowDecorationStyle.png

サンプルコード

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`を追加

`LookAndFeelを変更する場合は、ContentPane以下から更新することで、JRootPane#setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);が無効にならないようにする必要があります(MetalLookAndFeelのみLookAndFeel#getSupportsWindowDecorations()true`)。

for(Window window: Frame.getWindows()) {
  if(window instanceof RootPaneContainer) {
    RootPaneContainer rpc = (RootPaneContainer)window;
    SwingUtilities.updateComponentTreeUI(rpc.getContentPane());
  }
}

参考リンク

コメント