Java JFrame Size positionFrame(final JFrame frame, final Dimension viewSize, final int addX, final int addY)

Here you can find the source of positionFrame(final JFrame frame, final Dimension viewSize, final int addX, final int addY)

Description

Set the size and location of the frame.

License

Apache License

Parameter

Parameter Description
frame the frame to position.
viewSize the preferred size of the scrollable view.
addX the amount of width to add to the view.
addY the amount of height to add to the view.

Declaration

public static void positionFrame(final JFrame frame, final Dimension viewSize, final int addX, final int addY) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.Dimension;
import java.awt.DisplayMode;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import javax.swing.JFrame;

public class Main {
    /**/*from   w  w w .  j a  va  2  s .c o m*/
     * Set the size and location of the frame.
     *
     * @param frame    the frame to position.
     * @param viewSize the preferred size of the scrollable view.
     * @param addX     the amount of width to add to the view.
     * @param addY     the amount of height to add to the view.
     */
    public static void positionFrame(final JFrame frame, final Dimension viewSize, final int addX, final int addY) {
        final GraphicsDevice screenDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice();
        final DisplayMode displayMode = screenDevice.getDisplayMode();
        final Dimension screenSize = new Dimension(displayMode.getWidth(), displayMode.getHeight());

        final Dimension frameSize = new Dimension(Math.min(viewSize.width + addX, screenSize.width),
                Math.min(viewSize.height + addY, screenSize.height));

        final int frameX = Math.max((screenSize.width - frameSize.width) / 2, 0);
        final int frameY = Math.max((screenSize.height - frameSize.height) / 2, 0);

        frame.setSize(frameSize);
        frame.setLocation(frameX, frameY);
    }
}

Related

  1. getFrameSize(Component c)
  2. getFrameSize(Component c)
  3. isNormalSizeMode(JFrame window)
  4. locateWindow(JFrame parent, Dimension mySize, Dimension offset, JDialog me)
  5. lockInMinSize(final JFrame frame)
  6. resizeApp(JFrame app)
  7. restrictWindowMinimumSize(final JInternalFrame frame, final Dimension minSize)
  8. setMinMaxSizeFrame(JFrame frame)
  9. setMinSizeFrame(JFrame frame)