Resizes a JDialog by a given factor based on the client's screen size. - Java Swing

Java examples for Swing:JDialog

Description

Resizes a JDialog by a given factor based on the client's screen size.

Demo Code


//package com.java2s;

import java.awt.Dimension;

import java.awt.Toolkit;

import javax.swing.JDialog;

public class Main {
    /**/*  w ww.j a v a  2  s. c o  m*/
     * Resizes a {@link JDialog} by a given factor based on the client's screen
     * size. The screen is centered afterwards.
     * 
     * @param dialog
     * @param factor
     */
    public static void resizeAndCenter(JDialog dialog, double factor) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        dialog.setSize((int) Math.round(screenSize.width * factor),
                (int) Math.round(screenSize.height * factor));
        centerDialog(dialog);
    }

    /**
     * Centers the dialog on the screen.
     * 
     * @param dialog
     */
    public static void centerDialog(JDialog dialog) {
        dialog.setLocationRelativeTo(null);
    }
}

Related Tutorials