aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to set maxlength for java.awt.TextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to set maxlength for java.awt.TextField" Watch "How to set maxlength for java.awt.TextField" New topic
Author

How to set maxlength for java.awt.TextField

solomon alexander
Greenhorn

Joined: Mar 20, 2007
Posts: 19
Hi,

Could anyone let me know on how to set the maxlength for a field of type java.awt.TextField in Applet?

Thanks
Alex
solomon alexander
Greenhorn

Joined: Mar 20, 2007
Posts: 19
Hi,

Can anyone reply to my question?

Alex
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2503
    
    8

You can't. You need to detect it yourself using KeyListener/KeyAdapter or DocumentListener.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
bhagya hari
Greenhorn

Joined: Oct 15, 2008
Posts: 5
There is no direct method for this.

You can create a customized Textfield extends JTextField. In the constructor use the class which extends PlainDocument.
By overriding the method insertString in PlainDocument interface you can allow the user to enter only the specified number of character.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

"bhagya hb " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.


SCJP, SCWCD.
|Asking Good Questions|
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

K. Tsang and bhagya hari, I think both of you missed that it is an AWT TextField, not a JTextField.

Solomon, you could try using a TextListener. Or of course change your code to use Swing (and JApplet, JTextField). Not only does it look better, it's also a lot more powerful.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
solomon alexander
Greenhorn

Joined: Mar 20, 2007
Posts: 19
Hi Rob,

Thanks a lot. TextListener worked for me.

Regards
Solomon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set maxlength for java.awt.TextField