Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Akalya Baskaran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Akalya Baskaran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am totally new to Coding, and that is the reaon why I am struggling with this.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic