[Logo]
Forums Register Login
JTextfield not accepting "." when i tried to limit it to accept only numbers
Hi.. I am little familiar with GUI/Swing applications.Here i need a Jtextfield or a Jformattedfield for entering "rate" ,so it should accept only 0.00 format(2 decimal places).Please help me in getting this resolved.

(1 like)
Hi Akal,
Welcome to the Ranch

You have mentioned in the subject that your field is accepting numbers, but not ".". Can you show us what you have done so far?

The usual way to handle validation is to use a DocumentFilter. You can read about that here
Hi,

Here is the code which i tried for limiting numbers.

char key = evt.getKeyChar();

if(( key<'0' || key>'9') ){
evt.consume();
}
Hi,
You have provided very little information. I think you are using a KeyListener which is not the right way to do it. Did you go through the DocumentListener link that I provided earlier?
Hi,

You are right.I used Keylistener.I am trying it with DocumentListener.Also thanks for the link,its really very useful .I will let you know once I am done.
Don't use a DocumentListener, use a DocumentFilter as suggested. By using a DocumentListener the text will get inserted, only for it to be replaced again. If you're not careful you could get an infinite loop of changes. With a DocumentFilter you can modify the text before it's inserted.
 
Ranganathan Kaliyur Mannar wrote:Hi,
You have provided very little information. I think you are using a KeyListener which is not the right way to do it. Did you go through the DocumentListener link that I provided earlier?


I actually meant a DocumentFilter as I said initially. Sorry about the confusion.
Hi,

Can you please help me by giving me the sample code using DocumentFilter for the textfield that accepts only decimal numbers.
Actually the textfield is for entering rate of a product.
Am totally new to Coding, and that is the reaon why I am struggling with this.
 
Akal Boss wrote:Can you please help me by giving me the sample code using DocumentFilter for the textfield that accepts only decimal numbers.

Did you check out the link that I provided earlier? If you did, you should have seen a sample code. You can start from there.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 25, 2017 00:59:43.