Ensures a button has a specific minimum width, similar to what Windows does. - Java Swing

Java examples for Swing:JButton

Description

Ensures a button has a specific minimum width, similar to what Windows does.

Demo Code

/*/*from  w w w .  j  av a2 s. c o  m*/
 * 09/08/2005
 *
 * UIUtil.java - Utility methods for org.fife.ui classes.
 * Copyright (C) 2005 Robert Futrell
 * http://fifesoft.com/rtext
 * Licensed under a modified BSD license.
 * See the included license file for details.
 */
//package com.java2s;

import java.awt.Dimension;

import javax.swing.JButton;

public class Main {
    /**
     * Buttons look better when they have a minimum width.  Windows does this
     * automatically, for example.
     */
    private static final int DEFAULT_BUTTON_SIZE = 85;

    /**
     * Ensures a button has a specific minimum width, similar to what Windows
     * does.  This usually makes the UI look a little better, especially with
     * small buttons such as those displaying an "OK" label, for example.
     *
     * @param button The button to possibly elongate.
     * @see #ensureButtonWidth(JButton, int)
     */
    public static final void ensureDefaultButtonWidth(JButton button) {
        ensureButtonWidth(button, DEFAULT_BUTTON_SIZE);
    }

    /**
     * Ensures a button has a specific minimum width.  This can be useful if
     * you have a dialog with very small-labeled buttons, such as "OK", for
     * example.  Often, very small buttons look unprofessional, so artificially
     * widening them helps.
     *
     * @param button The button to possibly elongate.
     * @param width The minimum (preferred) width for the button.
     * @see #ensureDefaultButtonWidth(JButton)
     */
    public static final void ensureButtonWidth(JButton button, int width) {
        Dimension prefSize = button.getPreferredSize();
        if (prefSize.width < width) {
            prefSize.width = width;
            button.setPreferredSize(prefSize);
        }
    }
}

Related Tutorials