Java Swing How to - Move JFrame and update JDialog location








Question

We would like to know how to move JFrame and update JDialog location.

Answer

import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
//from w  w  w  . j a  va 2  s  . c o m
import javax.swing.JDialog;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JDialog dialog = new JDialog(frame, false);
    dialog.setSize(200, 50);
    frame.addComponentListener(new ComponentAdapter() {
      Point lastLocation;
      @Override
      public void componentMoved(ComponentEvent e) {
        if (lastLocation == null && frame.isVisible()) {
          lastLocation = frame.getLocation();
        } else {
          Point newLocation = frame.getLocation();
          int dx = newLocation.x - lastLocation.x;
          int dy = newLocation.y - lastLocation.y;
          dialog.setLocation(dialog.getX() + dx, dialog.getY() + dy);
          lastLocation = newLocation;
        }
      }
    });
    frame.setSize(400, 200);
    frame.setVisible(true);
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);
  }
}