• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JWindowをマウスで移動
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2004-09-06
*JWindowをマウスで移動 [#g9940ceb]
JWindowなどのタイトルバーのないフレームをマウスで移動します。

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

//#screenshot
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTL8cG8F0I/AAAAAAAAAYY/vZfyqnyr6-I/s800/DragWindow.png)

**サンプルコード [#r3a65b7b]
#code(link){{
public void createSplashScreen(String path) {
  ImageIcon img = new ImageIcon(getClass().getResource(path));
  DragWindowListener dwl = new DragWindowListener();
  splashLabel = new JLabel(img);
  splashLabel.addMouseListener(dwl);
  splashLabel.addMouseMotionListener(dwl);
  splashScreen = new JWindow(getFrame());
  splashScreen.getContentPane().add(splashLabel);
  splashScreen.pack();
  splashScreen.setLocationRelativeTo(null);
}
class DragWindowListener extends MouseAdapter {
  private MouseEvent start;
  //private Point  loc;
  private Window window;
  @Override public void mousePressed(MouseEvent me) {
    start = me;
  }
  @Override public void mouseDragged(MouseEvent me) {
    if(window==null) {
      window = SwingUtilities.windowForComponent(me.getComponent());
    }
    Point eventLocationOnScreen = me.getLocationOnScreen();
    window.setLocation(eventLocationOnScreen.x - start.getX(),
                       eventLocationOnScreen.y - start.getY());
    //loc = window.getLocation(loc);
    //int x = loc.x - start.getX() + me.getX();
    //int y = loc.y - start.getY() + me.getY();
    //window.setLocation(x, y);
  }
}
}}

**解説 [#p1806440]
JWindowや、setUndecorated(true)したJFrameのようにタイトルバーのないフレームをマウスのドラッグで移動します。実際はJWindowに、MouseMotionListenerなどを実装したコンポーネントを追加しています。

上記のサンプルでは、Splash ScreenのJLabelにリスナーを追加し、これをJWindowに貼り付けてドラッグできるようにしています。

次に開くsetUndecorated(true)したJFrameでは、青いラベル部分をドラッグすることが出来ます。

----
- マルチディスプレイなどで、別画面に移動できないバグ?を修正。
-- ただし、Web Start のSandBox内では、以前と同じく画面の外に移動することができない?
--- JNLP のセキュリティに all-permissions を設定する必要がある

- Swing Tutorial の [http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#FrameDemo2 FrameDemo2]で試しても、同様?
-- "Look and feel decorated": 画面外に移動不可
-- "Window system decorated": 画面外に移動可能

**参考リンク [#n4c8a267]
-[[JInternalFrameをJFrameとして表示する>Swing/InternalFrameTitleBar]]
-[[JFrameのタイトルバーなどの装飾を独自のものにカスタマイズする>Swing/CustomDecoratedFrame]]

**コメント [#hfe1ff82]
- JLabel無しでJFrameを直接つかんで移動させようと,ソース中のDragWindowListenerをFrameの引数に指定してaddMouseListener(),addMouseMotionListener()に追加してみたのですが,うまく動きませんでした。ラベルではなくフレームを直接つかんで移動させるにはどうすればよいのでしょうか? -- [[hshs]] &new{2013-03-14 (木) 08:00:42};
-- ``frame.getContentPane().addMouseListener(dwl);...``と、``ContentPane``か``RootPane``に``DragWindowListener``を追加するか、以下のように``DragWindowListener``を変更するのはどうでしょうか? -- [[aterai]] &new{2013-03-14 (木) 13:53:25};

#code{{
//package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainPanel extends JPanel{
  public MainPanel() {
    super(new BorderLayout());
    add(new JLabel("aaaaaaa"), BorderLayout.NORTH);
    add(new JButton(new AbstractAction("Exit") {
      @Override public void actionPerformed(ActionEvent e) {
        Window f = SwingUtilities.getWindowAncestor(
          (JComponent)e.getSource());
        f.dispatchEvent(
          new WindowEvent(f, WindowEvent.WINDOW_CLOSING));
      }
    }), BorderLayout.SOUTH);
  }
public class MainPanel{
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    DragWindowListener dwl = new DragWindowListener();
    JFrame frame = new JFrame();
    frame.setUndecorated(true);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.addMouseListener(dwl);
    frame.addMouseMotionListener(dwl);
    frame.getContentPane().add(new MainPanel());
    frame.setSize(320, 240);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}
class DragWindowListener extends MouseAdapter {
  private MouseEvent start;
  private Window window;
  @Override public void mousePressed(MouseEvent me) {
    start = me;
  }
  @Override public void mouseDragged(MouseEvent me) {
      Object o = me.getSource();
      if(o instanceof Window) {
          window = (Window)o;
      }else if(window==null && o instanceof JComponent) {
          window = SwingUtilities.windowForComponent(me.getComponent());
      }
    Point eventLocationOnScreen = me.getLocationOnScreen();
    window.setLocation(eventLocationOnScreen.x - start.getX(),
                       eventLocationOnScreen.y - start.getY());
  }
}
}}
- えーと、このソースは新たにJPanelを継承してるんですね、という事は結局、JFrame 自体をマウスドラッグでつかむことが出来ないということなんでしょうか? -- [[hshs]] &new{2013-03-15 (金) 11:08:52};
-- ``JFrame``の``ContentPane``に``JPanel``を継承したダミーコンポーネントをサンプルとして追加してるだけです。必要ないなら上記のように削除して(どこを削除したかはこのページ上にある「編集された箇所をみる」などで調べてください)調査してみるのがお手軽なのでオススメです。``JFrame``に追加したリスナーで``JFrame``の移動を行うことは可能ですが、本当に「``JFrame``自体をマウスドラッグでつかむことが出来ない」かどうかについては、内部での処理を追っていないのでよく分かりません。 -- [[aterai]] &new{2013-03-15 (金) 13:56:42};

#comment