Ensures a button has a specific minimum width. - Java Swing

Java examples for Swing:JButton

Description

Ensures a button has a specific minimum width.

Demo Code

/*/*from w  ww  .  ja  va 2  s  .  c om*/
 * 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 {
    /**
     * 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