Java JFrame Size setSizeWithinScreen(JFrame frame, int preferredWidth, int preferredHeight)

Here you can find the source of setSizeWithinScreen(JFrame frame, int preferredWidth, int preferredHeight)

Description

set Size Within Screen

License

Apache License

Declaration

public static void setSizeWithinScreen(JFrame frame, int preferredWidth, int preferredHeight) 

Method Source Code

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

import javax.swing.*;

import java.awt.*;

public class Main {
    public static void setSizeWithinScreen(JFrame frame, int preferredWidth, int preferredHeight) {
        Dimension screenSize = getMaxSize(frame);
        frame.setSize(new Dimension(Math.min(preferredWidth, screenSize.width),
                Math.min(preferredHeight, screenSize.height)));
    }// w w w . j ava 2 s  . c  o m

    public static Dimension getMaxSize(Window frame) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        GraphicsConfiguration config = frame.getGraphicsConfiguration();
        Dimension availableScreenSize = new Dimension(toolkit.getScreenSize());
        Insets insets = toolkit.getScreenInsets(config);
        availableScreenSize.width -= (insets.left + insets.right);
        availableScreenSize.height -= (insets.top + insets.bottom);
        return availableScreenSize;
    }

    private static Insets getScreenInsets(Window window) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        GraphicsConfiguration config = window.getGraphicsConfiguration();
        return toolkit.getScreenInsets(config);
    }
}

Related

  1. resizeApp(JFrame app)
  2. restrictWindowMinimumSize(final JInternalFrame frame, final Dimension minSize)
  3. setMinMaxSizeFrame(JFrame frame)
  4. setMinSizeFrame(JFrame frame)
  5. setSizeBasedOnResolution(final JFrame frame)
  6. showSplashWindow(String message, Font messageFont, int duration, Dimension windowSize, Window frameOwner)