swing - Accept only numbers and a dot in Java TextField -
i've got 1 textfield accept numbers keyboard, have change it's "price textfield" , need accept dot "." kind of prices.
how can change in order need?
ptominimofield = new jtextfield(); ptominimofield.setbounds(348, 177, 167, 20); contentpanel.add(ptominimofield); ptominimofield.setcolumns(10); ptominimofield.addkeylistener(new keyadapter() { public void keytyped(keyevent e) { char caracter = e.getkeychar(); if (((caracter < '0') || (caracter > '9')) && (caracter != '\b')) { e.consume(); } } });
as suggested oracle ,use formatted text fields
formatted text fields provide way developers specify valid set of characters can typed in text field.
amountformat = numberformat.getnumberinstance(); ... amountfield = new jformattedtextfield(amountformat); amountfield.setvalue(new double(amount)); amountfield.setcolumns(10); amountfield.addpropertychangelistener("value", this);
Comments
Post a Comment