Creates a text field that prefers the supplied width. - Java Swing

Java examples for Swing:JTextField

Description

Creates a text field that prefers the supplied width.

Demo Code

// http://github.com/threerings/nexus/blob/master/LICENSE
//package com.java2s;
import java.awt.Dimension;
import javax.swing.JTextField;

public class Main {
    /**//from   w w  w  . j  av a2 s  . c  o m
     * Creates a text field that prefers the supplied width.
     */
    public static JTextField newTextField(final int width) {
        // that this is not achievable more simply, simply boggles the mind
        return new JTextField() {
            public Dimension getPreferredSize() {
                Dimension d = super.getPreferredSize();
                d.width = Math.max(width, d.width);
                return d;
            }
        };
    }
}

Related Tutorials