• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Key Filtering problem

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic