0

I'm trying to set the size of JTextField, but for some reason it stays the same size and fills up the whole JPanel, I am using setPreferredSize, but this makes no difference:

JPanel loginJPanel = new JPanel(new BorderLayout());
JTextField usernameJTextField = new JTextField();
usernameJTextField.setPreferredSize(new Dimension(50, 100));
loginJPanel.add(usernameJTextField);
juergen d
  • 201,996
  • 37
  • 293
  • 362

2 Answers2

1

It does make a difference, but the layout may choose to ignore preferred size settings. The center area of BorderLayout gets as much of the available space as possible. See How to Use BorderLayout for more details.

Consider this example that packs the frame, as a result the preferred size of the text field is respected. But once the frame is resized, the text field is resized as well.

import javax.swing.*;
import java.awt.*;

class Demo {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel loginJPanel = new JPanel(new BorderLayout());
                JTextField usernameJTextField = new JTextField();
                usernameJTextField.setPreferredSize(new Dimension(50, 100));
                loginJPanel.add(usernameJTextField);

                JFrame frame = new JFrame();
                frame.add(loginJPanel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}

Take a look at Visual Guide to Layout Managers and perhaps you would find a more suitable layout for your needs.

Also, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?.

EDIT:

Note that you're usually encouraged to specify the number of columns when initializing text fields. This number is used to calculate preferred width. For example textField = new JTextField(20); See How to Use Text Fields for more details:

If you do not specify the number of columns or a preferred size, then the field's preferred size changes whenever the text changes, which can result in unwanted layout updates.

Community
  • 1
  • 1
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • 1
    I fixed it by making it a `FlowLayout`. Thanks, –  May 06 '14 at 21:47
  • @HarryKitchener, you have asked multiple layout questions. Each time you've been given a link to the Swing tutorial. Several times you have also be given the link on why NOT to use setPreferredSize(). I hope you will finally read the tutorial so you don't have to keep posting basic layout questions. – camickr May 07 '14 at 01:32
  • please 1.) .... new JTextField(15); , 2.) frame.setLocationByPlatform(true); (maybe doesnt important in this case, but any locations for...) should be after code line pack() – mKorbel May 07 '14 at 06:38
  • @mKorbel I included these details in the edit. Although, `JTextField` initialization snippet is actually based on OP's posted code. The idea of the sample was to show that in this case the preferred size is respected (specifying number of columns would probably override the specified preferred size). Thank you for your input! – tenorsax May 07 '14 at 07:09
0

Since you set layout manager of your jpanel to BorderLayout, it adds jtextfield to center by default. Use a null layout instead.

JPanel loginJPanel = new JPanel(null);
jdiver
  • 2,228
  • 1
  • 19
  • 20