Java Utililty Methods JFrame Center

List of utility methods to do JFrame Center

Description

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

Method

voidalignCenter(JInternalFrame internalFrame)
align Center
JDesktopPane desktopPane = internalFrame.getDesktopPane();
int desktopWidth = desktopPane.getWidth();
int desktopHeight = desktopPane.getHeight();
int frameWidth = internalFrame.getWidth();
int frameHeight = internalFrame.getHeight();
Point newLocation = new Point((desktopWidth - frameWidth) / 2, (desktopHeight - frameHeight) / 2);
internalFrame.setLocation(newLocation);
voidcenter(JFrame frame)
Center JFrame.
Dimension frameSize = frame.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((screenSize.width - frameSize.width) >> 1, (screenSize.height - frameSize.height) >> 1);
voidcenter(JFrame frame)
Description of the Method
Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((int) ((screen_size.getWidth() - frame.getWidth()) / 2),
        (int) ((screen_size.getHeight() - frame.getHeight()) / 2));
voidcenter(JFrame parent, JInternalFrame dialog)
Centers the supplied dialog in its parent's bounds.
Dimension psize = parent.getSize();
Dimension dsize = dialog.getSize();
dialog.setLocation((psize.width - dsize.width) / 2, (psize.height - dsize.height) / 2);
voidcenterAndSizeFrame(JFrame frame)
Centeres the frame on the user's screen and sets the size of it to a reasonable size.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int height = screenSize.height;
int width = screenSize.width;
frame.setSize(width / 2, height / 2);
frame.setLocationRelativeTo(null);
DimensioncenterBigFrame(JFrame frame, int maxWidth, int maxHeight, double scaling, int minHeight)
Center a big window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int winWidth = screenSize.width - 40;
int winHeight = screenSize.height - 150;
if (winWidth > maxWidth) {
    winWidth = maxWidth;
if (winHeight > maxHeight) {
    winHeight = maxHeight;
...
voidcenterDialog(final JFrame frame, JDialog dialog)
Centers the dialog above the given frame
int parWidth = frame.getWidth();
int parHeight = frame.getHeight();
int parX = frame.getLocation().x;
int parY = frame.getLocation().y;
Point loc = new Point(((parWidth - dialog.getWidth()) / 2) + parX,
        ((parHeight - dialog.getHeight()) / 2) + parY);
dialog.setLocation(loc);
voidcenterDialog(JDialog dialog, JFrame frame)
Centers a dialog in a frame
if (dialog == null) {
    throw new NullPointerException("The dialog can't be null");
if (frame == null) {
    throw new NullPointerException("The frame can't be null");
final Dimension d = dialog.getSize();
dialog.setLocationRelativeTo(frame);
...
voidcenterDialog(JDialog dialog, JFrame parent)
center Dialog
Point p = parent.getLocationOnScreen();
p.x += ((parent.getWidth() - dialog.getWidth()) / 2);
p.y += ((parent.getHeight() - dialog.getHeight()) / 2);
if (p.y <= 0)
    p.y = 20;
if (p.x <= 0)
    p.x = 20;
dialog.setLocation(p);
...
voidcenterDialogIntoFrame(java.awt.Component p_CompToBePositioned, javax.swing.JFrame p_MainFrame)
center Dialog Into Frame
Dimension l_size = p_CompToBePositioned.getSize();
int y = p_MainFrame.getY() + ((p_MainFrame.getHeight() / 2) - (l_size.height / 2));
int x = p_MainFrame.getX() + ((p_MainFrame.getWidth() / 2) - (l_size.width / 2));
p_CompToBePositioned.setLocation(x, y);