Here you can find the source of setSizeWithinScreen(JFrame frame, int preferredWidth, int preferredHeight)
public static void setSizeWithinScreen(JFrame frame, int preferredWidth, int preferredHeight)
//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); } }