aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Restricted JTextField problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Restricted JTextField problem" Watch "Restricted JTextField problem" New topic
Author

Restricted JTextField problem

Sam Woodhams
Greenhorn

Joined: Sep 01, 2009
Posts: 3
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

Joined: Oct 27, 2005
Posts: 19552
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Samuel Woodhams
Ranch Hand

Joined: Sep 01, 2009
Posts: 31
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

Joined: Jul 26, 2007
Posts: 9993
    
    7

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Samuel Woodhams
Ranch Hand

Joined: Sep 01, 2009
Posts: 31
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

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

Joined: Sep 01, 2009
Posts: 31
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

Joined: Sep 01, 2009
Posts: 31


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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Restricted JTextField problem
 
Similar Threads
Running a Method inside a ActionListener
Mortgage Calculator -In Frames
What is/the point of, a secure thread?
GroupLayout not working in a JPanel
How to clear a JTextField!!