This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Fixing Max. char in JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Fixing Max. char in JTextField" Watch "Fixing Max. char in JTextField" New topic

Fixing Max. char in JTextField

Manoj Tyagi
Ranch Hand

Joined: Jun 11, 2002
Posts: 35
Hi to all,
I am working on JTextField & want that my JTextField should not take more than 1(or any fixed no.) chatacter. How to make this restriction.
I tried setColumns(1) method but it did nothing.
Please help.
Manoj Tyagi
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You need to create a document. Take a look at the sun tutorial on how to use them. If you don't have any luck post back.
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
You can extend the JTextField and implement the Key Listener interface. In the keyTeyped() method, u can check the length and consume the keystrokes if the length >= 1.

Joy is a radiation
I agree. Here's the link:
subject: Fixing Max. char in JTextField
It's not a secret anymore!