This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spaces into a JtextField

 
Steeve Randria
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I would like to know how to count the number of spaces in a JTextField which is limited in characters. For example, if I have a textField limited to 6 characters and I type the letter A , it should return me: A + 5 space because otherwise I'm forced to enter them manually.

How can i implement that please?

Here is my plainDocument:



Thanks a lot for your help.
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The section from the Swing tutorial o Implementing a Document Filter shows how to limit the number of characters in any text component.
 
Steeve Randria
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thank you for your help, but I can already limit the number of character.
I want fill my JTextField with spaces if text entered is less than the maximum number of characters allowed.

I tried this:

But it does not work (i can type only one character)

Thanks for your help.

 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quite understand your requirement. There's a confusion between the contents of the text field at the time the user is entering data and the contents after that time.

To put it another way: if the user hasn't entered anything yet, should the value of the field be 6 blanks? And then what happens when the user tries to type a letter? Should it go at the beginning? Should the user be able to insert it at the end, after the 6 blanks? In the middle?

If you just want to pad the data with blanks to 6 characters after the user finishes entering the data, that's an entirely different (and much easier) question. So what I'm asking is, does the user have to be bothered with this padding requirement while they are entering the data, or does it only apply after they have finished?
 
Steeve Randria
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your help

or does it only apply after they have finished?


Yes, that is what I want : pad data with blank after the user finishes entering data.
For exemple , if the max limit into my jtextfield is 6 characters and the user type the letter A, the value of the field must be A + 5 blanks , and if he hasn ' t entered anything yet , the value of the field must be 6 blanks.

Thanks a lot
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So do that padding after retrieving the text from the field and before processing it further. String#format(...) may be useful for that.
 
Steeve Randria
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks for your help,

I have tried this and it works:


but i don ' t know if it ' s the better way to proceed(work only with one textField)
If somebody has a better solution let me know!

Thanks
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the documentation for Formatter.
 
Steeve Randria
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot
It works perfectly now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic