wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTextfield not accepting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextfield not accepting "." when i tried to limit it to accept only numbers" Watch "JTextfield not accepting "." when i tried to limit it to accept only numbers" New topic
Author

JTextfield not accepting "." when i tried to limit it to accept only numbers

Akalya Baskaran
Greenhorn

Joined: Nov 15, 2012
Posts: 7
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.

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1083
    
  10

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


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Akalya Baskaran
Greenhorn

Joined: Nov 15, 2012
Posts: 7
Hi,

Here is the code which i tried for limiting numbers.

char key = evt.getKeyChar();

if(( key<'0' || key>'9') ){
evt.consume();
}
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1083
    
  10

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?
Akalya Baskaran
Greenhorn

Joined: Nov 15, 2012
Posts: 7
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1083
    
  10

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.
Akalya Baskaran
Greenhorn

Joined: Nov 15, 2012
Posts: 7
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.
Akalya Baskaran
Greenhorn

Joined: Nov 15, 2012
Posts: 7
Am totally new to Coding, and that is the reaon why I am struggling with this.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1083
    
  10

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextfield not accepting "." when i tried to limit it to accept only numbers