Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Restricted JTextField problem

 
Sam Woodhams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing I want to say is that I’m brand new to java so with that in mind here is my problem.

I have a small app that basically generates a random string of characters that the user can use as a password. The length of the password can be defined by the user and here lies the problem. I want the input of the JTextfield to be restricted so that only numbers can be entered and that it’s limited to 3 characters. As it stands the code can do that, however it does not do it how I would like it to. The entire mask has to be filled in by the user, so for example the value 1 has to put in as 001 which is not what I want to be. Also it is possible to have gaps in between the numbers like 1 5 which causes it to error. I’ve removed it silently erroring by adding a workaround but all it does is that if that error happens it just defaults to 000.



i'll include class Genarate and class Copy just incase you want to run the entire app





thanks in advance for helping

sam
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out DocumentFilter instead. With that, you can determine for each character or even complete strings whether or not you want to allow them, or even modify them before inserting.
 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks very much for the quick reply (i've got a new account coz the other one stopped working)

i've looked up as much info as possible on DocumentFilter and if mange to get this code working



and this is the DocumentFilter


this code works fine for limiting the input to 3 characters, but i want only a numerical imput. can i use the DocumentFilter to do this and if so how?

thanks in advance
sam
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree this is not directly related to you current problem, but if possible, try and consider a JSpinner with a SpinnerNumberModel instead of a text field. From the usability point of view, it would be more suitable.
 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the quick reply and yes i had conciddered using a JSpinner but thats not the effect i want for this.

i was conciddering puting in a second if command before the first to determin if it is a number being inputed and if not then it rejecting the input but im not sure how to tell the if command to look for somthing like that.

sam
 
Rob Camick
Ranch Hand
Posts: 2584
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i want only a numerical imput. can i use the DocumentFilter to do this and if so how?


You have to loop through all the characters in the input string to make sure they are numeric. Look at the Character class which has a method you can use to check for numeric digits.
 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Rob.

i had a gd look at Character Classes and i hope i've under stood it proppolly.
i made some changes to my code so that should let it work how i want it to,

The idea is that the code checks to see if the number inputed is a number, and if it is then checks to see if this would make the string too long. however every input (including numerical ones) throws the first error message, saying it is an ilegal character. i think i have in correctlly writen the syntex that checks the input but i can't get the program to compile any other way. if someone can point out were i've made the mistake (or if i have completely misunderstood what i should do) can you point it out please.

thanks in advance
sam
 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


sorry everyone i just noticed why it wasn't working. i was trying to use \b as the filter for numbers, but i should have been using \\d

thanks very much to everyone who helped me solve this problem

the finished DocumentFilter just incase anyone comes across this thread and wants the same kind of DocumentFilter

Thanks to everyone again
sam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic