简体   繁体   中英

How to check if a text contains only int values in Java?

I want to determine if JTextField.getText() contains only numbers between 0-9, where anything else is not accepted such as letters and other characters. What's a good way to do this? thanks.

If you are using NetBeans to build your GUI, you just need to put regular JTextFields in your GUI and in the creation code, you will specify the constructor of IntegerField.

OR

Use JFormattedTextField capabilities.

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

Have a look at example( http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM