Formatted text field

J

John M

Hello,

how is it possible to enable only numbers input in a jFormattedTextField?
I use now this code:
JFormattedTextField jFormattedTextField1 = new JFormattedTextField(new
DecimalFormat());

But in this case user can enter any character and the decimal conversion
comes after leaving the cell.
I want that user can enter only digits.

Thanks!
 
S

Sudsy

John said:
Hello,

how is it possible to enable only numbers input in a jFormattedTextField?
I use now this code:
JFormattedTextField jFormattedTextField1 = new JFormattedTextField(new
DecimalFormat());

But in this case user can enter any character and the decimal conversion
comes after leaving the cell.
I want that user can enter only digits.

Thanks!

You need to refer to the javadocs first! There's an example of using
an InputVerifier in the JFormattedTextField page which address
precisely what you are asking. Save the bandwidth for the thorny
ones, please.
 
A

Andrew Thompson

John M said:
Hello,

how is it possible to enable only numbers input in a jFormattedTextField?
I use now this code:
JFormattedTextField jFormattedTextField1 = new JFormattedTextField(new
DecimalFormat());

But in this case user can enter any character and the decimal conversion
comes after leaving the cell.
I want that user can enter only digits.

Som is '-1' valid for your field, it is an integer,
but does not consist of only digits..

If you _only_ need digits, you might try this..

Add a keylistener to a textfield. Parse the
contents as an integer on each keypress, if
that throws an exception get the text of the
field, trim it by 1, put it back and inform the user..

[ This has some problems if the user types
987, then arrows back 9 and types a,
but I think you get the idea.. ]

HTH
 
A

ak

how is it possible to enable only numbers input in a jFormattedTextField?

you can have only numbers also in JTextField.
Use Document which accepts only digits. It is much better then KeyListener.


____________

http://reader.imagero.com the best java image reader.
 
A

Andrew Thompson

ak said:
jFormattedTextField?

you can have only numbers also in JTextField.
Use Document which accepts only digits. It is much better then
KeyListener.

This was about integers, right?

'-3' and '1.73E+3' are both integers,
...aren't they (shrugs)?
 
A

ak

you can have only numbers also in JTextField.
KeyListener.

This was about integers, right?

'-3' and '1.73E+3' are both integers,
..aren't they (shrugs)?
ok, not only digits. Use DecimalFormat _in_ Document of JTextField.

____________

http://reader.imagero.com the best java image reader.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top