Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Questions about JTextField  RSS feed

 
Sally Jenkins
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be a strange question but...

Is it possible to a Restrict a user to enter number (e.g., integer, double) data into a JTextField?

If not, should I just convert the string to an integer or double after the user enters the data?

Please Note: This is just a question. I did some reading and was a bit curious as to the JTextField limitations in regard to requesting a user's information such as age.
 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to that is yes, but also a question back at you: how much time to you want to take writing an edit for it? You can always make your input go through an edit before hitting or being finalized in the destination object.

Sally Jenkins wrote:This may be a strange question but...

Is it possible to a Restrict a user to enter number (e.g., integer, double) data into a JTextField?

If not, should I just convert the string to an integer or double after the user enters the data?

Please Note: This is just a question. I did some reading and was a bit curious as to the JTextField limitations in regard to requesting a user's information such as age.
 
Norm Radder
Ranch Foreman
Posts: 2218
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to a Restrict a user to enter number (e.g., integer, double) data into a JTextField?

Yes. There are underlying classes that can control what characters can be entered.
 
Campbell Ritchie
Sheriff
Posts: 55348
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you search this forum, there are quite a few threads about how to tell whether a String represents a number. I found this and this; you can doubtless find more. I think the first link is better.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sally Jenkins wrote:
Please Note: This is just a question. I did some reading and was a bit curious as to the JTextField limitations in regard to requesting a user's information such as age.


For requirements such as age, you can considering a JSpinner paired with a backing SpinnerNumberModel.
More on it here https://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html

On the other had if you HAVE to use a JTextField, check out this wiki entry. It shows you how to restrict input by manipulating the associated document model
http://www.coderanch.com/t/659988/Wiki/Numeric-Text-Field

PS. I am crossposting this question to the Swing forum for you.
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
On the other had if you HAVE to use a JTextField, check out this wiki entry. It shows you how to restrict input by manipulating the associated document model
http://www.coderanch.com/t/659988/Wiki/Numeric-Text-Field


I have two minor comments on the code at that link:

  • This is the old way of doing it, and it still works. But since java1.4 (2002) the typical approach would be to use a DocumentFilter.
  • Why @SuppressWarnings("serial") instead of simply providing a serialVersionUID?

  • However I didn't see any way to add these comments. It appears to be a wiki page, so I could have edited the entire page, but I thought that would be impolite to the page's author.
     
    Sally Jenkins
    Greenhorn
    Posts: 17
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well I really appreciate the feedback. To be honest I don't want to do a lot of editing to make JTextField work. However, for Age the idea of JSpinner is a great idea because age has a realistic numerical range.

    However, it does appear that to acquire a number through JTextField you would need to just parseint() the string and then work to handle any possible exceptions.

    However I do have another question in regard to Norm's comment what underlying classes are you referring? Could anyone give me an example?
     
    Rob Camick
    Ranch Hand
    Posts: 2787
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Use a JFormattedTextField.

    See How to Use Formatted Text Fields.

    Bookmark the tutorial for a resource to Swing basics.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!