Search code examples
javaswinguser-interfacejtextfield

JTextField getting cutoff without expanding window


I have a Java Swing application which has a JTextField inside it. I want to assign a minimum width to the field so it is expanded no matter what. I even tried starting it with some text inside the field to no avail.

Edit: The Layout is a GridBagLayout for that particular area.

The <code>JTextField</code> is being smooshed unless I expand the window more.


Solution

  • It depends on what layout you are using but try

    TextField.setPreferredSize(new Dimension(150, 23));
    

    or initialize the text field with

    JTextField TextField = new JTextField(20);