Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, Swing: how do I set the maximum width of a JTextField?

I'm writing a custom file selection component. In my UI, first the user clicks a button, which pops a JFileChooser; when it is closed, the absolute path of the selected file is written to a JTextField.

The problem is, absolute paths are usually long, which causes the text field to enlarge, making its container too wide.

I've tried this, but it didn't do anything, the text field is still too wide:

fileNameTextField.setMaximumSize(new java.awt.Dimension(450, 2147483647));

Currently, when it is empty, it is already 400px long, because of GridBagConstraints attached to it.

I'd like it to be like text fields in HTML pages, which have a fixed size and do not enlarge when the input is too long.

So, how do I set the max size for a JTextField ?

like image 736
Leonel Avatar asked Sep 24 '08 15:09

Leonel


People also ask

How do I change the size of a textfield in Java Swing?

textField. setColumns(...); to let the text field determine the preferred width. Or if you want the width to be the entire width of the parent panel then you need to use an appropriate layout.

How do I limit the number of characters in a JTextField?

addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { if (txtGuess. getText(). length() >= 3 ) // limit textfield to 3 characters e.

Which method is used to set the size of the component in pixels?

You may use setSize() method to set the size of JTextField component.

Which method is used to set or change the text in JTextField?

The java. awt. TextField used the text of the field as the command string for the ActionEvent . JTextField will use the command string set with the setActionCommand method if not null , otherwise it will use the text of the field as a compatibility with java.


2 Answers

It may depend on the layout manager your text field is in. Some layout managers expand and some do not. Some expand only in some cases, others always.

I'm assuming you're doing

filedNameTextField = new JTextField(80); // 80 == columns

If so, for most reasonable layouts, the field should not change size (at least, it shouldn't grow). Often layout managers behave badly when put into JScrollPanes.

In my experience, trying to control the sizes via setMaximumSize and setPreferredWidth and so on are precarious at best. Swing decided on its own with the layout manager and there's little you can do about it.

All that being said, I have no had the problem you are experiencing, which leads me to believe that some judicious use of a layout manager will solve the problem.

like image 178
davetron5000 Avatar answered Sep 19 '22 00:09

davetron5000


I solved this by setting the maximum width on the container of the text field, using setMaximumSize.

According to davetron's answer, this is a fragile solution, because the layout manager might disregard that property. In my case, the container is the top-most, and in a first test it worked.

like image 27
Leonel Avatar answered Sep 20 '22 00:09

Leonel