Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Book Number text field.

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can we create text field component in which only the digits can be entered? for example, book number text field only receives "0"~"9",it refuses any character other than "0"~"9". do we need to create a event to handle the problem, or just use a given component(which component?)?
thanks,
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hao,
By changing the Document model of the textfield. You can subclass JTextField and override the createDefaultModel method to return a Document that only accepts 0-9. For the Document that is returned you can subclass PlainDocument and overried insertString to actually restrict the chars entered into the field.
So you could do something like this:

Hope this helps,
Michael Morris
 
hao li
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,
but need a little change!
class SeatsDocument should be static.
thank you again!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


... class SeatsDocument should be static


Why? In my submission, both of the classes were actually private inner classes of my booking panel. If your intention is to make available the SeatsDocument class (and presumably have SeatsField as a public class) then I might be willing to concede the point. But since SeatsDocument is specifically targeted to SeatsField, it seems to me that it should still be a private inner class and if so, what's the point of its being static?
Michael Morris
 
hao li
Greenhorn
Posts: 22
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Orignially posted by Michael Morris:
If your intention is to make available the SeatsDocument class (and presumably have SeatsField as a public class) then I might be willing to concede the point.


What did I say?
[ August 28, 2002: Message edited by: Michael Morris ]
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
First of all, i agree with michael. Obviously i did i different. I just tried to parse the contents of the textfield to an integer. If that works, then everything is fine. If it does not then an exception is thrown and caught of course. This allows me to give a good notification to the user of what he/she did wrong.
It is just a different approach.
regards,
friso
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my Document that I used to only allow numbers.

And here's my extended JTextField

Mark
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friso,
Coincidently I did the same. But there is one problem. If the number entered is greater than the range of intiger, it still gives the error, and that's where I am facing the problem. Cause even if user entered 9 digit numeric value this give NumberFormatException and hence my next logic to display error dialog executes, which just says "Please enter numeric value only". So I guess the best way to do this is by extending JTextField.
Or if there is any way to stop user after first 8 digits are entered, then it will also work. I dont know any way to do so. Does anybody know?
Thanks,
Manoj
 
Ranch Hand
Posts: 854
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nine numeric digits? Good gravy, how big are your planes? Personally, I'm stopping at three digits and I think that's a bit much.
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I've used a JSpinner that gets the value of available seats and set's that value to the max number allowed for input and a minimum of 0!
It's a simple soluction and avoid problems like having 35 seats and the user tries to book 36 seats.
Miguel
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Burk,
It seems that people talking of a 9 digit numeric value are speaking of a 9 digit customer ID.
Miguel, in such a context, I think that JSpinner solution is not applicable.
Best,
Phil.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:
this.bookField = new JFormattedTextField(new
MaskFormatter("########"));

bookField.setFocusLostBehavior(JFormattedTextField.PERSIST);
 
Manoj Gundawar
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill,
I will try this. I read about this in the API, but they dont have examples there. It is still pending. So I was not daring to try that. And another disadvantage was of loosing focus. But with your solution, it seems it will work.
And yes, to clarify the the confusion, this is for contractor assignment and the field is for customer id.
Thanks,
Manoj
 
Manoj Gundawar
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(the same discussion was going on in the other post, which was closed. But I had put further quereis there, which were uniqe. But as the post was closed, I am carrying forward the same query here...)
Vlad, thanks for the code. It helped me to grasp the concept too.
But later I realized that this will not solve my problem, as I am using
JOptionPane.showInputDialog. This was a very easy option.
But as this shows its own default textfield, I could not find a way to setDocument to it's default textfield.
If I try to add a new textfield to this, it shows 2 text field, one my own and 2nd it's default one.
Is there any way to set the Document to the showInputDialog's default textfield? I could not find anything in the API.
The other option to create the customized optionpane involves too much of code, which I dont want to do just for this small purpose.

And thanks to Bill too for the code snippet. I guess now instead of setDocument one could (or should) use the JFormattedTextField, as this is in the new API (1.4) I am not sure if it is ok, when one says that his/her assignment is developed on the latest version (like 1.4.1 or 2) and still use the setDocument and not the JFormattedTextField.
Thanks,
Manoj
[ October 08, 2003: Message edited by: Jmannu gundawar ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I tried this idea: overriding the method insertString and it seems to work in general, BUT ...

sometimes my app gets frozen in

super.insertString(offset,str,a);


the parameters looks OK,

offset = 0
str = a valid string
a = null


Have you got any idea what could be the problem?

Thanks,
Attila
 
bacon. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic