Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a standard way of creating a text field that requires the input to be an integer?  RSS feed

 
Jon Swanson
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been googling this and as is typical of Java, have found at least ten suggestions, all different and spanning about 8 years.

I have a GUI with a text field a want the field to be limited to integer values. The simplest approach seemed to be:



This ALMOST works.

The first box is restricted to a number, I put "ee" in the box and hit <enter> and a bell sounds and if I leave the field, the value reverts to the last number that was in there.

The second box is supposed to be an IntegerInstance. If I put "5.2" in the box and press <enter> no bells, no nothing. So the user gets no feedback that "5.2" is not appropriate. If I leave the box, the number changes to "5".

So this can't be the right way to do this.

I saw another method that was a bit more complicated.



Another ALMOST. I can turn the number red to indicate something is wrong and emit a beep, but not when the user hits <enter> only when I switch to another field. A bit too late. I think There is also the problem that if I correct the number, the number stays red until I leave the box, <enter> will not cause the input verifier to verify the input.

Now, I am thinking this is something a lot of people need to do, so the language should support it. But I'm not finding the solution. What do most people do?

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Swanson wrote:Now, I am thinking this is something a lot of people need to do, so the language should support it. But I'm not finding the solution. What do most people do?

Well I'm no GUI expert, but I reckon I'd probably extend JTextField to do something like:
and call value = toInteger(getText()) any time it's updated. If value is null after the call, then the text entered was invalid; so force them to keep updating it until they get it right.

Winston

Edit: Looking at your examples, my idea seems pretty close to Number 2; although I think I'd just extend an existing text field type if possible.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A DocumentFilter is one of the best ways to prevent any invalid content for text components. If you use the search you will find several examples.
 
Jon Swanson
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I will look into the document filter. I found one example so far, the same post also suggested this route:



what was added was the shouldYieldFocus override and an actionListener. The action listener gets the text box to respond on <enter>. I added the DoubleVerifier because the behavior of the FormattedTextField was OK, but now the behavior of both integer and double fields is exactly the same, I issue a beep and turn the font red to indicate a problem. Also, I allow a null field, which the FormattedTextField did not.

So except for a bunch more lines of code I am not feeling too bad about the solution. Though it scares me a bit that this needs to be done by any programmer needing to validate integers.

Does it look sensible or is the document filter a clearly superior route?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have a look at the Swing FAQ (Swing forum main page)
 
Jon Swanson
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael, I was googling and using search in the forum and didn't pick up the FAQ by either method. It looks easier than I thought. Though it just brings on the next question, since I want the same behavior from the fields where floating point numbers are being entered as I would have with integer numbers, I'll need to see if I can make validating floating point numbers work using a document. At least what I have now is consistent for different fields and is close to the behavior I have when adding numbers to a table. Anyway, gives me more to think about.
 
Jon Swanson
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this link to be useful-

example of floating point text field
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!