Set Frame Location Relative To : Frame « Swing « Java Tutorial






import javax.swing.*;
import java.awt.*;

/**
 * @author Adrian BER (beradrian@yahoo.com)
 */
public class UIUtilities {


    public static void setFrameLocationRelativeTo(JFrame f, Component c) {
        if (c == null) {
            // Setting the position of the dialog on the center of the screen
            // in 1.4 should be replaced by
//             f.setLocationRelativeTo(null);
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            f.setLocation((int)d.getWidth()/2 - (int)f.getPreferredSize().getWidth()/2,
                    (int)d.getHeight()/2 - (int)f.getPreferredSize().getHeight()/2);
        }
    }
}








14.82.Frame
14.82.1.Get all windows with Frame.getFrames()
14.82.2.Set Frame Location Relative To
14.82.3.Get Owning Frame for Component