Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

set a char limit in TextField

 
Ubaldo Robledo
Greenhorn
Posts: 22
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, i need to set a limit when a textfield is typed, i dont know where to start to do this validate.

do you know a way to do it. or can you take me to the correct path.??

thankyou!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing to do is define, very specifically what you mean by 'set a limit'. You don't have to define it to us but you need to understand exactly what the rules are yourself. Then you can look for 'textfield validation' as a search term. The InputVerifier might help you out with that, but it might depend on the rules, you might need a KeyListener on the text field.
 
Robert D. Smith
Ranch Hand
Posts: 216
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good place to start would be the Java Docs for String.

Regards,
Robert

(edit to clear up a silly misinterpretation of the question)
 
Ubaldo Robledo
Greenhorn
Posts: 22
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, thanks,

I'll tell you,, what's limit? i want to set the textfield size to n value.

example n=10

so when i type in the textfield, only allows me to write 10 characters, I took a loo in the web. and i found some info.
but i tryied and it doesn't work. may can you help me.

i set to my textfield a Document and i pass the limit value, and the textfield. i extend plainDocument class, but i doesnt understand it very well.



 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's usually the example given in tutorials about Swing text fields, isn't it? And it sounds like you already found one of those examples -- but maybe it isn't working the way you hoped? If so, then why don't you post what you have so far and explain what your problem is. You know, what it's supposed to do, what it does instead, what exceptions are thrown, and so on.
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. i extend plainDocument class,


That is the old way to do this.

Read the section from the Swing tutorial on Implementing a Document Filter for a working example using the newer approach.
 
Ubaldo Robledo
Greenhorn
Posts: 22
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:That's usually the example given in tutorials about Swing text fields, isn't it? And it sounds like you already found one of those examples -- but maybe it isn't working the way you hoped? If so, then why don't you post what you have so far and explain what your problem is. You know, what it's supposed to do, what it does instead, what exceptions are thrown, and so on.


ok, this is te declaration of the variable and im seting a document.



and this is a class which extends from PlainDocument



tutorial says that when i type one char in the text field if is lower than maxC (max Value Allowed) it'll be written, else if the char isbigger than mi maxC (maxValueAllowed) it doesnt. (sorry i dont write english very well, hope you understand)

thats it.


Rob Camick wrote:
That is the old way to do this.

Read the section from the Swing tutorial on Implementing a Document Filter for a working example using the newer approach.


Rob i'll try also this way, thanks, i'll tell you if i can make it work (i'm a rockie on this)

thanks guys
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!