I have four JTextFields where user is allowed to enter only numbers.
Question is how do I do this?

Recommended Answers

You could use the keypressed event handler

http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html

or wait till the user enters the entire lot then parse it at the end returning either a success or fail message.

Jump to Post

You could use component like a dropdown box to limit the users choices to a range of numbers.

Jump to Post

I think that limiting user input is always good.

How about the JSpinner class. That should allow the user to input a large range of numbers (that you specify in the constructor).

Jump to Post

All 9 Replies

Second option is simpler. I thought I could be clever and discover something new, but it is not my day today :cheesy:
I will check user input with Character.isDigit() and kick of error message if anything wrong

You could use component like a dropdown box to limit the users choices to a range of numbers.

I don't think so . . .
They are allow only to numbers but whole range of numbers, well zero onwards. Drop down box is good if you code calendar and don't know any other technique to do it(as their better then this) or you want to give them certaine values

I think that limiting user input is always good.

How about the JSpinner class. That should allow the user to input a large range of numbers (that you specify in the constructor).

You can use a javax.swing.JFormattedTextField with a java.text.NumberFormat as the format. For example:

numberField = new JFormattedTextField(NumberFormat.getInstance());

Guys please, problem solved no need to continue with posting.
Thank you for understanding

@peter budo The quickest solution is what titaniumdecoy suggested...Use a JFormattedTextField to restrict input to numbers. Not sure why some of the other solutions here contain a book of code to do such a simple task.

Hi P.P.
Welcome to DaniWeb
Did you notice this thread was marked "solved" 6 years ago? Your contribution is welcome, but it's far too late to be relevant.
JC

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.