Is there any way to accept only numeric values in a JTextField? Is there any special method for this?
numberField = new JFormattedTextField(NumberFormat.getInstance());
You would like to take a look at JFormattedTextField
Formatted text fields provide a way for developers to specify the valid set of characters that can be typed in a text field
This is a subclass of JTextField, so you can use it like this:
JTextField textField = new JFormattedTextField(NumberFormat.getInstance());
Although there is the pure evil JFormattedTextField
there isn't a trivial way to do it using only the Swing library. The best way to implement this sort of feature is with a DocumentFilter
.
I think it is the best solution:
JTextField textField = new JFormattedTextField(new MaskFormatter("###")); //
You can create a beautiful text field in java that accepts or allows only numeric values.. You can even set the precision for the float values... check the code in zybocodes(www.zybonics.com/zybocodes/ http://www.zybonics.com/zybocodes/code/CodeViewForm.php?codeID=3