Java JComponent Size setSizeBasedOnResolution(final JComponent component)

Here you can find the source of setSizeBasedOnResolution(final JComponent component)

Description

Set the size of the JComponent based on the resolution of the screen

License

Open Source License

Parameter

Parameter Description
component a parameter

Declaration

public static void setSizeBasedOnResolution(final JComponent component) 

Method Source Code


//package com.java2s;
import javax.swing.*;
import java.awt.*;

public class Main {
    /**/*  ww  w.j  ava  2s . c  o m*/
     * Set the size of the JComponent based on the resolution of the screen
     *
     * @param component
     */
    public static void setSizeBasedOnResolution(final JComponent component) {
        Dimension dimension = getDimensionBasedOnResolution();
        component.setSize(dimension.width, dimension.height);
    }

    /**
     * Set the size of the JFrame based on the resolution of the screen
     *
     * @param frame
     */
    public static void setSizeBasedOnResolution(final JFrame frame) {
        Dimension dimension = getDimensionBasedOnResolution();
        frame.setSize(dimension.width, dimension.height);
    }

    /**
     * @return a Dimension object containing the resolution of the first display iff it exists
     *         800 x 600 will be returned as a default
     */
    private static Dimension getDimensionBasedOnResolution() {
        int width = 800;
        int height = 600;
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
        if (graphicsDevices.length >= 1) {
            DisplayMode displayMode = graphicsDevices[0].getDisplayMode();
            width = displayMode.getWidth() - 60;
            height = displayMode.getHeight() - 60;
        }
        return new Dimension(width, height);
    }
}

Related

  1. setSize(JComponent comp, int w, int h)
  2. setSize(JComponent componenet, Dimension dimension)
  3. setSize(JComponent componenet, Dimension dimension)
  4. setSize(JComponent component, int width, int height)
  5. setSize(JComponent conponent, int width, int height)
  6. setSizes(JComponent[] components, final Dimension dimension)
  7. setSMPSizes(JComponent comp, Dimension d)
  8. setUnlimitedSize(JComponent component)
  9. setZeroMinimumSize(Component component)