• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*JFrameの移動を同期 [#qa088c78]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:&date~
更新日:&lastmod;
---
category: swing
folder: DockingFrames
title: JFrameの移動を同期
tags: [JFrame, ComponentListener]
author: aterai
pubdate: 2005-07-04T07:42:08+09:00
description: JFrameを2つ並べて作成し、その位置関係を保ったまま移動できるようにします。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTLtEL3M0I/AAAAAAAAAYA/9HmyXI1Uw0M/s800/DockingFrames.png
---
* 概要 [#summary]
`JFrame`を`2`つ並べて作成し、その位置関係を保ったまま移動できるようにします。

#contents
**概要 [#r16f61f8]
JFrameを2つ並べて作成し、その位置関係を保ったまま移動できるようにします。
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTLtEL3M0I/AAAAAAAAAYA/9HmyXI1Uw0M/s800/DockingFrames.png)

http://terai.xrea.jp/swing/dockingframes/screenshot.png
* サンプルコード [#sourcecode]
#code(link){{
private void positionFrames(ComponentEvent e) {
  if (e.getComponent().equals(frame1)) {
    int x = frame1.getBounds().x;
    int y = frame1.getBounds().y + frame1.getBounds().height;
    frame2.removeComponentListener(this);
    frame2.setLocation(x, y);
    frame2.addComponentListener(this);
  } else {
    int x = frame2.getBounds().x;
    int y = frame2.getBounds().y - frame1.getBounds().height;
    frame1.removeComponentListener(this);
    frame1.setLocation(x, y);
    frame1.addComponentListener(this);
  }
}
}}

**サンプルコード [#f580c5f3]
 private void positionFrames(ComponentEvent e) {
   if(e.getSource().equals(frame1)) {
     int x = frame1.getBounds().x;
     int y = frame1.getBounds().y + frame1.getBounds().height;
     frame2.removeComponentListener(this);
     frame2.setLocation(x, y);
     frame2.addComponentListener(this);
   }else{
     int x = frame2.getBounds().x;
     int y = frame2.getBounds().y - frame1.getBounds().height;
     frame1.removeComponentListener(this);
     frame1.setLocation(x, y);
     frame1.addComponentListener(this);
   }
 }
* 解説 [#explanation]
- `JFrame`を上下に並べてそれぞれに`ComponentListener`を実装したリスナーを追加
- 片方のフレームが移動された時、残りのフレームの位置を指定する前に一旦このリスナーを削除することで処理ループの発生を防止

-[[サンプルを起動>http://terai.xrea.jp/swing/dockingframes/sample.jnlp]]
-[[jarファイル>http://terai.xrea.jp/swing/dockingframes/sample.jar]]
-[[ソース>http://terai.xrea.jp/swing/dockingframes/src.zip]]
* 参考リンク [#reference]
- [https://community.oracle.com/thread/1479997 Swing (Archive) - how to dock two jdialogs?]
- [https://community.oracle.com/thread/1492552 Swing (Archive) - how to catch drag event in the title bar of a jframe]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/event/ComponentListener.html ComponentListener (Java Platform SE 8)]

**解説 [#ceedca5e]
上記のサンプルでは、JFrameを上下に並べて、それぞれにComponentListenerインターフェイスを実装したリスナーを追加しています。片方のフレームが移動された時、残りのフレームの位置を指定する前に、一旦このリスナーを削除してやることで、処理がループしないようになっています。
**参考リンク [#q31d1b00]
-[[Java Forums - how to dock two jdialogs?>http://forum.java.sun.com/thread.jspa?forumID=57&threadID=545918]]
-[[Java Forums - how to catch drag event in the title bar of a jframe>http://forum.java.sun.com/thread.jspa?forumID=57&threadID=429463]]

**コメント [#ic655f15]
* コメント [#comment]
#comment
#comment