0

I have a situation where a JTextField should only be allowed to have 80 characters. The JTextField can grow with the frame. I would like the JTextField to grow but only to a size where it cam accomodate 80 characters even if the frame is expanded more.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

2
JPanel textConstrain = new JPanel(new FlowLayout()); // will respect maximum size
JTextField maxSizeField = new JTextField(80);
textConstrain.add(maxSizeField);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

You can use jtextField.setColumns(columns); It accepts the number of columns i.e. number of characters. You can either set it to 80 so that the size will always be suitable for 80 character or else you can listen to text change event and as per the length of the entered character you can change the number of columns of JTextField by calling setColumns(inputText.length);

Hope this helps.

Ameer Tamboli
  • 1,218
  • 12
  • 20