Have a
jtextfield
in my java form. I want to not allow to type alpha(Characters) and only allow to type numbers.And can't type 2 decimal point and two numbers after the decimal point.(that
jtextfield
for price..)
Please tell me how to do this step by step.
Try this code if you want only number to be inputed in you
textfield
paste the above code in your constructor after
txtfield
initializationalso take a look at this stuff JFormattedTextField for Double still takes characters
Use
JFormattedTextField
with NumberFormat (See for example). Alternatively you can add your ownDocumentFilter
to the document of yourJTextField
use JFormattedTextField with NumberFormatter
restrict number of decimal places
set various RoundingModes
restrict range, set minimal and/or maximal value
another of ways is to use JSpinner with SPinnerNUmberModel, but required to use DocumentFilter
for example