Java JDialog adaptSize(JDialog frame, int preferredWidth, int preferredHeight)

Here you can find the source of adaptSize(JDialog frame, int preferredWidth, int preferredHeight)

Description

Set the size of the dialog to the given size, or the size of the screen if it the screen is too small.

License

Open Source License

Parameter

Parameter Description
frame a parameter
preferredWidth a parameter
preferredHeight a parameter

Declaration

public static void adaptSize(JDialog frame, int preferredWidth, int preferredHeight) 

Method Source Code

//package com.java2s;
/*/*  ww  w . ja v  a  2  s .  c  o  m*/
 * Spirit, a study/biosample management tool for research.
 * Copyright (C) 2018 Idorsia Pharmaceuticals Ltd., Hegenheimermattweg 91,
 * CH-4123 Allschwil, Switzerland.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * @author Joel Freyss
 */

import java.awt.Dimension;

import javax.swing.JDialog;
import javax.swing.JFrame;

public class Main {
    /**
     * Set the size of the frame to the given size, or the size of the screen if it the screen is too small.
     * If the size is bigger than the screen, the frame is maximized
     * @param frame
     * @param preferredWidth
     * @param preferredHeight
     */
    public static void adaptSize(JFrame frame, int preferredWidth, int preferredHeight) {
        java.awt.Toolkit toolkit = frame.getToolkit();
        Dimension sSize = toolkit.getScreenSize();
        if (preferredWidth > 0 && preferredHeight > 0) {
            frame.setSize(Math.min(preferredWidth, sSize.width - 30), Math.min(preferredHeight, sSize.height - 30));
            if (preferredWidth > sSize.width && preferredHeight > sSize.height) {
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            }
        } else {
            frame.pack();
        }
        frame.setLocationRelativeTo(null);

    }

    /**
     * Set the size of the dialog to the given size, or the size of the screen if it the screen is too small.    *
     * @param frame
     * @param preferredWidth
     * @param preferredHeight
     */
    public static void adaptSize(JDialog frame, int preferredWidth, int preferredHeight) {
        java.awt.Toolkit toolkit = frame.getToolkit();
        Dimension sSize = toolkit.getScreenSize();
        if (preferredWidth > 0 && preferredHeight > 0) {
            frame.setSize(Math.min(preferredWidth, sSize.width - 30), Math.min(preferredHeight, sSize.height - 30));
        } else {
            frame.pack();
        }
        frame.setLocationRelativeTo(null);
    }
}

Related

  1. addFileOnAction(AbstractButton button, final JTextField textField, final JDialog parent)
  2. alignDialogInMiddleOfScreen(JDialog dlg)
  3. applyDefaultProperties(final JDialog comp)
  4. calculatePosition(Window parent, JDialog dialog)