TITLE:JWindowをマウスで移動
Posted by terai at 2004-09-06

JWindowをマウスで移動

JWindowなどのタイトルバーのないフレームをマウスで移動します。
  • category: swing folder: DragWindow title: JWindowをマウスで移動 tags: [JWindow, JFrame, MouseListener, MouseMotionListener] author: aterai pubdate: 2004-09-06T00:58:19+09:00 description: JWindowなどのタイトルバーのないフレームをマウスで移動します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTL8cG8F0I/AAAAAAAAAYY/vZfyqnyr6-I/s800/DragWindow.png

概要

JWindowなどのタイトルバーのないフレームをマウスで移動します。

#screenshot

サンプルコード

#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
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);
}
#spandel
class DragWindowListener extends MouseInputAdapter { // JDK 6 -> MouseAdapter {
#spanend
  private MouseEvent start;
  private Point  loc;
#spanadd

#spanend
#spanadd
class DragWindowListener extends MouseAdapter {
#spanend
  private final Point startPt = new Point();
  private Window window;
  public void mousePressed(MouseEvent me) {
    start = me;
#spanadd

#spanend
  @Override public void mousePressed(MouseEvent me) {
    startPt.setLocation(me.getPoint());
  }
  public void mouseDragged(MouseEvent me) {
    if(window==null) {
#spanadd

#spanend
  @Override public void mouseDragged(MouseEvent me) {
    if (window == null) {
      window = SwingUtilities.windowForComponent(me.getComponent());
    }
    loc = window.getLocation(loc);
    int x = loc.x - start.getX() + me.getX();
    int y = loc.y - start.getY() + me.getY();
    window.setLocation(x, y);
    Point eventLocationOnScreen = me.getLocationOnScreen();
    window.setLocation(eventLocationOnScreen.x - startPt.x,
                       eventLocationOnScreen.y - startPt.y);
    // loc = window.getLocation(loc);
    // int x = loc.x - start.getX() + me.getX();
    // int y = loc.y - start.getY() + me.getY();
    // window.setLocation(x, y);
  }
}

解説

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

解説

JWindowsetUndecorated(true)したJFrameのようにタイトルバーのないフレームをマウスのドラッグで移動します。実際はJWindow自体にリスナーを設定するのではなく子コンポーネントにMouseMotionListenerなどを追加しています。 上記のサンプルでは、Splash ScreenのJLabelにリスナーを追加し、これをJWindowに貼り付けてドラッグできるようにしています。 上記のサンプルではJLabelにリスナーを追加し、これをJWindowに配置してドラッグ可能にしています。 次に開くsetUndecorated(true)したJFrameでは、青いラベル部分をドラッグすることが出来ます。
  • スプラッシュスクリーンの次に開くJFrameJFrame#setUndecorated(true)を設定してタイトルバーなどは非表示
  • 代わりに青いラベル部分をドラッグ可能に設定

コメント

  • -
  • マルチディスプレイなどで別画面に移動できないバグ?を修正
    • ただしWeb StartSandBox内では以前と同じく画面の外までは移動不可?
      • JNLPのセキュリティにall-permissionsを設定する必要がある
  • Swing TutorialFrameDemo2で試しても、同様?
    • Look and feel decorated: 画面外に移動不可
    • Window system decorated: 画面外に移動可能

参考リンク

コメント