• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Restricted JTextField problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic