Example usage for javax.swing JDialog getX

List of usage examples for javax.swing JDialog getX

Introduction

In this page you can find the example usage for javax.swing JDialog getX.

Prototype

public int getX() 

Source Link

Document

Returns the current x coordinate of the components origin.

Usage

From source file:Main.java

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);//from ww w . j  a  v  a 2 s.  c o  m
    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);
}