• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to set the size of a JTextField ?

 
Avin Sinanan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I was wondering how to limit the size of a JTextfield to say 5 characters so that onlt 5 letters can fit.
The code does not work ;
JTextField field = new JTextField(5) ;
why doesn't this work?
any suggestions?
yours respectfully Avin Sinanan
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 5 in the constructor does not refer to characters but columns. It is used to calculate the preferred size. Unless a monospaced font is used, that space can be taken up differently by say a "W" and a "i". If you want the size different, set the preferred size yourself.
It also does not limit the entry to 5. You need to create a document to handle that. Take a look at the API example and modify it for your use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!