• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JInternalFrameをJFrameとして表示する
#navi(../)
#tags(JFrame, JInternalFrame, MouseListener, MouseMotionListener)
RIGHT:Posted by &author(aterai); at 2009-08-31
*JInternalFrameをJFrameとして表示する [#tf5e5ced]
``JFrame``のタイトルバーなどを非表示にし、``JInternalFrame``のタイトルバーでこれらを代用します。
---
category: swing
folder: InternalFrameTitleBar
title: JInternalFrameをJFrameとして表示する
tags: [JFrame, JInternalFrame, MouseListener, MouseMotionListener]
author: aterai
pubdate: 2009-08-31T15:27:18+09:00
description: JFrameのタイトルバーなどを非表示にし、JInternalFrameのタイトルバーでこれらを代用します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTOo9LcVwI/AAAAAAAAAcs/fUEpKhXr_aI/s800/InternalFrameTitleBar.png
hreflang:
    href: https://java-swing-tips.blogspot.com/2009/11/add-jinternalframe-to-undecorated.html
    lang: en
---
* 概要 [#summary]
`JFrame`のタイトルバーなどを非表示にし、`JInternalFrame`のタイトルバーでこれらを代用します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTOo9LcVwI/AAAAAAAAAcs/fUEpKhXr_aI/s800/InternalFrameTitleBar.png)

//#screenshot
#ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTOo9LcVwI/AAAAAAAAAcs/fUEpKhXr_aI/s800/InternalFrameTitleBar.png)

**サンプルコード [#k636189e]
* サンプルコード [#sourcecode]
#code(link){{
final JInternalFrame internal = new JInternalFrame("@title@");
BasicInternalFrameUI ui = (BasicInternalFrameUI)internal.getUI();
JInternalFrame internal = new JInternalFrame("@title@");
BasicInternalFrameUI ui = (BasicInternalFrameUI) internal.getUI();
Component title = ui.getNorthPane();
for(MouseMotionListener l:title.getListeners(MouseMotionListener.class)) {
for (MouseMotionListener l: title.getListeners(MouseMotionListener.class)) {
  title.removeMouseMotionListener(l);
}
DragWindowListener dwl = new DragWindowListener();
title.addMouseListener(dwl);
title.addMouseMotionListener(dwl);
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(new JTree()));
p.add(new JButton(new AbstractAction("close") {
  @Override public void actionPerformed(ActionEvent e) {
    Window w = SwingUtilities.windowForComponent((Component)e.getSource());
    //w.dispose();
    Window w = SwingUtilities.windowForComponent((Component) e.getSource());
    // w.dispose();
    w.getToolkit().getSystemEventQueue().postEvent(
      new WindowEvent(w, WindowEvent.WINDOW_CLOSING));
  }
}), BorderLayout.SOUTH);
internal.getContentPane().add(p);
internal.setVisible(true);

KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
focusManager.addPropertyChangeListener(new PropertyChangeListener() {
  @Override public void propertyChange(PropertyChangeEvent e) {
    String prop = e.getPropertyName();
    if("activeWindow".equals(prop)) {
      try{
        internal.setSelected(e.getNewValue()!=null);
      }catch(PropertyVetoException ex) {
        ex.printStackTrace();
      }
focusManager.addPropertyChangeListener(e -> {
  String prop = e.getPropertyName();
  if ("activeWindow".equals(prop)) {
    try {
      internal.setSelected(Objects.nonNull(e.getNewValue()));
    } catch (PropertyVetoException ex) {
      throw new IllegalStateException(ex);
    }
  }
});
}}

**解説 [#d219693c]
上記のサンプルでは、``JInternalFrame``のタイトルバーを使用することで、タイトルバーに閉じるボタンのないフレームを作成しています。
* 解説 [#explanation]
上記のサンプルでは、`JInternalFrame`のタイトルバーを使用することでタイトルバーに閉じるボタンのないフレームを作成しています。

-``JFrame#setUndecorated(true)``で、``JFrame``のタイトルバーなどを非表示
-``BasicInternalFrameUI#getNorthPane()``で``JInternalFrame``のタイトルバーを取得
--元の``MouseMotionListener``を削除
--``JInternalFrame``をドラッグすると親の``JFrame``が移動する``MouseMotionListener``を追加
- `JFrame#setUndecorated(true)`で`JFrame`のタイトルバーなどを非表示
- `BasicInternalFrameUI#getNorthPane()`で`JInternalFrame`のタイトルバーを取得
-- 元の`MouseMotionListener`を削除
-- `JInternalFrame`をドラッグすると親の`JFrame`が移動する`MouseMotionListener`を追加
- `JDK 1.7.0`以上の場合は`frame.setBackground(new Color(0x0, true)); frame.add(p = new MainPanel()); p.setOpaque(false);`で角の透明化が可能
- 最大化、最小化、リサイズなどには未対応
-- KBD{Alt+Space}で最大化、最小化できるが、元のサイズに戻せなくなる場合がある
- %%角の透明化には未対応%%
-- 目立たなくするために、`LookAndFeel`は`Nimbus`に変更

-制限
--最大化、最小化、リサイズなどには未対応
---``Alt+Space``で最大化、最小化できるが、元のサイズに戻せなくなる場合がある
--角の透明化には未対応
---目立たなくするために、``LookAndFeel``は``Nimbus``に変更
---``JDK 1.7.0``なら、``frame.setBackground(new Color(0,0,0,0)); frame.add(p=new MainPanel()); p.setOpaque(false);``で角の透明化が可能
* 参考リンク [#reference]
- [[JWindowをマウスで移動>Swing/DragWindow]]
- [[JFrameのタイトルバーなどの装飾を独自のものにカスタマイズする>Swing/CustomDecoratedFrame]]
- [[JRootPaneにリサイズのための装飾を設定する>Swing/WindowDecorationStyle]]

**参考リンク [#y4fccd48]
-[[JWindowをマウスで移動>Swing/DragWindow]]
-[[JFrameのタイトルバーなどの装飾を独自のものにカスタマイズする>Swing/CustomDecoratedFrame]]
* コメント [#comment]
#comment
- `JFrame`のアクティブ状態が変わったら、`JInternalFrame`の選択状態も変化するように変更。 -- &user(aterai); &new{2009-11-13 (金) 14:57:18};
- リサイズ可能?にする場合のテスト。 -- &user(aterai); &new{2010-06-10 (木) 15:07:18};
-- [[JRootPaneにリサイズのための装飾を設定する>Swing/WindowDecorationStyle]]に移動。 -- &user(aterai); &new{2013-06-17 (月) 02:29:47};

**コメント [#b720b8d7]
- ``JFrame``のアクティブ状態が変わったら、``JInternalFrame``の選択状態も変化するように変更。 -- [[aterai]] &new{2009-11-13 (金) 14:57:18};
- リサイズ可能?にする場合のテスト -- [[aterai]] &new{2010-06-10 (木) 15:07:18};

#code{{
//package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class MainPanel extends JPanel{
    public MainPanel() {
        super(new BorderLayout());
        add(makeUI());
        // Translucent resize area for mouse cursor >>>
        setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
        setBackground(new Color(0,0,1,0.01f));
        //<<<
        setPreferredSize(new Dimension(320, 240));
    }
    public JComponent makeUI() {
        final JInternalFrame internal = new JInternalFrame("@title@");
        BasicInternalFrameUI ui = (BasicInternalFrameUI)internal.getUI();
        Component title = ui.getNorthPane();
        for (MouseMotionListener l:title.getListeners(MouseMotionListener.class)) {
            title.removeMouseMotionListener(l);
        }
        DragWindowListener dwl = new DragWindowListener();
        title.addMouseListener(dwl);
        title.addMouseMotionListener(dwl);
        JPanel p = new JPanel(new BorderLayout());
        p.add(new JScrollPane(new JTree()));
        p.add(new JButton(new AbstractAction("close") {
            @Override public void actionPerformed(ActionEvent e) {
                Window w = SwingUtilities.windowForComponent((Component)e.getSource());
                //w.dispose();
                w.dispatchEvent(new WindowEvent(w, WindowEvent.WINDOW_CLOSING));
            }
        }), BorderLayout.SOUTH);
        internal.getContentPane().add(p);
        internal.setVisible(true);

        KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        focusManager.addPropertyChangeListener(new PropertyChangeListener() {
            @Override public void propertyChange(PropertyChangeEvent e) {
                String prop = e.getPropertyName();
                //System.out.println(prop);
                if ("activeWindow".equals(prop)) {
                    try {
                        internal.setSelected(e.getNewValue()!=null);
                    } catch (PropertyVetoException ex) {
                        ex.printStackTrace();
                    }
                    //System.out.println("---------------------");
                }
            }
        });
        return internal;
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        JFrame frame = new JFrame();
        try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            for(UIManager.LookAndFeelInfo laf:UIManager.getInstalledLookAndFeels()) {
                if("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        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);

        frame.setMinimumSize(new Dimension(300, 120));
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.setBackground(new Color(0,0,0,0)); //JDK 1.7
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
class DragWindowListener extends MouseAdapter {
    private MouseEvent start;
    private Window window;
    @Override public void mousePressed(MouseEvent me) {
        if(window==null) {
            Object o = me.getSource();
            if(o instanceof Window) {
                window = (Window)o;
            }else if(o instanceof JComponent) {
                window = SwingUtilities.windowForComponent(me.getComponent());
            }
        }
        start = me;
    }
    @Override public void mouseDragged(MouseEvent me) {
        if (window!=null) {
            Point pt = new Point(); pt = window.getLocation(pt);
            int x = pt.x - start.getX() + me.getX();
            int y = pt.y - start.getY() + me.getY();
            window.setLocation(x, y);
        }
    }
}
}}

#comment