aspose file tools*
The moose likes Java in General and the fly likes Key Filtering problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Key Filtering problem" Watch "Key Filtering problem" New topic
Author

Key Filtering problem

Diego Pedrosa
Greenhorn

Joined: Nov 09, 2007
Posts: 8
Hello all,

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...

Thanks


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
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
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.
Diego Pedrosa
Greenhorn

Joined: Nov 09, 2007
Posts: 8
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 ]
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
I read up on it - not all of this will be totally useful, but you are stuck.

So at the risk of posting my first attempt at a REGEX to an SCJP:

[Diego Pedrosa:] &nbsop; -- 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

It is not documented which KeyEvent user should consume to leave text component unchanged.

In this particular case the cause of the problem is that we use Swing text components in XAWT and these components are updated by KEY_TYPED.

See: Bug ID: 5046125 REGRESSION: Textfield.keyPressed().KeyEvent.consume() fails to consume key event

"[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."
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Ummmm... sure, Nick. Whatever you say.

Diego, your regex looks fine; the only change you'd want to make would be to change that second "+" (one or more digits after the dot) to a "*" (zero or more), since otherwise "123." doesn't match.


[Jess in Action][AskingGoodQuestions]
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[EFH:]   Ummmm... sure, Nick. Whatever you say.

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 ?
Diego Pedrosa
Greenhorn

Joined: Nov 09, 2007
Posts: 8
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 ]
Diego Pedrosa
Greenhorn

Joined: Nov 09, 2007
Posts: 8
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... :-(
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923


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.


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Key Filtering problem