Java Utililty Methods JDialog Center

List of utility methods to do JDialog Center

Description

The list of methods to do JDialog Center are organized into topic(s).

Method

voidcenterOn(JDialog dialog, Component other)
center On
int w = dialog.getWidth();
int h = dialog.getHeight();
Rectangle bounds = other.getBounds();
int bw = bounds.width;
int bh = bounds.height;
dialog.setLocation(bounds.x + (bw - w) / 2, bounds.y + (bh - h) / 2);
voidcenterOnComponent(JDialog dialog, Component comp)
center On Component
if (comp == null || !comp.isShowing()) {
    centerOnScreen(dialog);
    return;
Rectangle b = comp.getBounds();
Point p = comp.getLocationOnScreen();
dialog.setLocation(p.x + (b.width - dialog.getWidth()) / 2, p.y + (b.height - dialog.getHeight()) / 2);
voidcenterOnScreen(JDialog jdialog)
center On Screen
int w = jdialog.getWidth();
int h = jdialog.getHeight();
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle bounds = env.getMaximumWindowBounds();
int bw = bounds.width;
int bh = bounds.height;
jdialog.setLocation((bw - w) / 2, (bh - h) / 2);
voidcenterWindow(JDialog dialog)
center Window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dialogSize = dialog.getSize();
if (dialogSize.height > screenSize.height) {
    dialogSize.height = screenSize.height;
if (dialogSize.width > screenSize.width) {
    dialogSize.width = screenSize.width;
dialog.setLocation((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2);
voidcenterWindow(JDialog dialog)
center Window
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = dialog.getSize().width;
int h = dialog.getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
dialog.setLocation(x, y);
voidcentrarVentana(JDialog pDialog)
centrar Ventana
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension objectSize = pDialog.getSize();
if (objectSize.height > screenSize.height) {
    objectSize.height = screenSize.height;
if (objectSize.width > screenSize.width) {
    objectSize.width = screenSize.width;
pDialog.setLocation((((screenSize.width - objectSize.width) / 2)),
        (screenSize.height - objectSize.height) / 2);
PointgetCentralDialogLocation(Component parent, JDialog dialog)
get Central Dialog Location
Point parentLocation = parent.getLocation();
Dimension parentSize = parent.getSize();
Dimension dialogSize = dialog.getSize();
int x = Math.max(0, parentLocation.x + parentSize.width / 2 - dialogSize.width / 2);
int y = Math.max(0, parentLocation.y + parentSize.height / 2 - dialogSize.height / 2);
return new Point(x, y);
RectanglegetCentredDialogBounds(JDialog dialog, Component parent, int defaultWidth, int defaultHeight)
get Centred Dialog Bounds
Dimension pref = dialog.getPreferredSize();
int w = pref.width;
int h = pref.height;
if (defaultWidth > 0)
    w = defaultWidth;
if (defaultHeight > 0)
    h = defaultHeight;
Rectangle parentBounds = parent.getBounds();
...
voidpackAndCenterJDialog(JDialog form)
pack And Center J Dialog
form.pack();
form.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width) / 2 - form.getWidth() / 2,
        (Toolkit.getDefaultToolkit().getScreenSize().height) / 2 - form.getHeight() / 2);
voidpackInCenter(JDialog dialog)
pack the dialog in the center of the screen
dialog.pack();
Point center = getLocation(null);
int x = center.x - dialog.getWidth() / 2;
int y = center.y - dialog.getHeight() / 2;
if (x < 20) {
    x = 20;
if (y < 20) {
...