I've been fighting with this problem for a couple of days...
I have some text fields that can only accept number (actually doubles, so "0-9 && ".") to later do some calculations.
What I did try: -- Using the KeyPressed event (a la VB) to skip anything out of the pattern show above, result: couldn't null the keycode/keychar nor consume the key pressed
-- Using FormattedTextField, but i dont need an input "mask", and it was VERY clumsy (there are easier ways) to implement a filter.
-- Using Patterns as suggested in the "Code Barn" (thanks guys for putting it there), it is almost what i need... now i can't find a regex that works... I looked at Regular Expression Library but when i run the app the fields won't let me enter the dot, or more than 1 copy of a given number.
Does someone has any idea on how to do this? 'couse my brain has already gone down the sink...
SCJP 5.0 (Yeah!! finally...)<br /> <br />There are 10 kinds of people...<br />The ones that understand binary,<br />and the ones that don't. ;P
Try setting the Document object of the text field to a custom document that overrides the insertString method. In that method, validate the input and ignore it if it's not a valid value.
Joined: Nov 09, 2007
That's exactly what I'm doing, the problem is that I'm using regexp in the inserString() method...
Mabye I could use a simple string filter="0123456789." with the filter.contains(str) method and let in if true... ...though the regexp is more elegant IMHO... [ November 14, 2007: Message edited by: Diego Pedrosa ]
"[Diego Pedrosa:] &nbsop; ^\\d+(\\.\\d+)?$"; <---- THIS is my problem (can't find a good one)
Notes: Try leaving out the $, it actuall picks up the line terminator in regression testing and lookingAt() give up what it found without passing through what did not match later in the text input. Also note I used your construct \. to match literal '.' I did not find otherwise in the book. Use group()Returns the input subsequence matched by the previous match. [ November 14, 2007: Message edited by: Nicholas Jordan ]
"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
I took everything (except my clearly caveated rewrite) off of the sun server, on a link which largely I copy pasted into the reply. Are you saying the Sun Bug report is not current ?
[EFH:] change that second "+" (one or more digits after the dot) to a "*" (zero or more), since otherwise "123." doesn't match.
But would it not have matched up to that point ?
Joined: Nov 09, 2007
Thanks guys... I'll try that as soon as I go back to program the GUI (now i got some change requests on the model, like "start over" jaja). Ernest: my impression is that I won't be able to input more than 1 copy of each number... that's my actual problem... I'll let you know (as I gotta try it).
Anyway... THANKS again both. [ November 21, 2007: Message edited by: Diego Pedrosa ]
Joined: Nov 09, 2007
Ok, I confirmed that the problem persists... I can only type 1 time each number including the dot.
Example: i can put this 46375.12098, but it won't let me do this: 11.00
I think I'll try with a regular old fashioned, "a la VB", filter... :-(
Joined: Sep 17, 2006
I thought about this some more, and my 'regex' ( remember it is my first one ) has to have some sort of multiplicity -> * ? + which I missed but what snags me here is where is public void insertString(int offs, String str, AttributeSet a) in your program logic and what is it overriding ? The double digit thing I will have to leave for later, first figuing out where ( in the flow of logic ) you are calling the code you have here, then further if(! tmp.contains(str)) has a not operator: What I see this code saying is that if insertString(int offs, String str, AttributeSet a) does not contain pattern, then call the superclass version,... huh ? I think we have some recurrent calls here and cross-locked logic.
Would it not be String str = (TextField).getText(); if(matches(pattern)) super.insertText(str),... and so on. I am missing something here d+ ought to match some digits buy why this thing is shorting on multple of same thing makes sense to me only if there is some more code we need to see.
Note how it works: If you type, the string which is inserted is allways one Character long, and that String is guarded for matching the regular expression. So you can't avoid multiple dots (127.0.0.1) in the String without looking at the previous String. Of course people like to copy and paste numbers they found somewhere else (maybe at google [is there a google:numbers already?]), so maybe they try to insert more than one digit/dot at once.
I guess you find a more elegant solution than mine.