aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Spaces into a JtextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Spaces into a JtextField" Watch "Spaces into a JtextField" New topic
Author

Spaces into a JtextField

Steeve Randria
Greenhorn

Joined: Sep 17, 2010
Posts: 16
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

Joined: Jun 13, 2009
Posts: 2189
    
    7
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

Joined: Sep 17, 2010
Posts: 16
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Sep 17, 2010
Posts: 16
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

Joined: May 03, 2008
Posts: 4547
    
    5

So do that padding after retrieving the text from the field and before processing it further. String#format(...) may be useful for that.


luck, db
There are no new questions, but there may be new answers.
Steeve Randria
Greenhorn

Joined: Sep 17, 2010
Posts: 16
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

Joined: May 03, 2008
Posts: 4547
    
    5

Read the documentation for Formatter.
Steeve Randria
Greenhorn

Joined: Sep 17, 2010
Posts: 16
Thanks a lot
It works perfectly now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spaces into a JtextField