• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt about jTextField and keypressed

 
gus gol
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello mates,
I need that my program to doesn't allow typing "," or "."

Example, you are typing a word in a jTextField and then you press the "," or "." button in your keyboard, so the program shows a JOptionPane saying "You cant type , or ."

Is that possible?? Thanks ^^
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gus gol wrote:Hello mates,
I need that my program to doesn't allow typing "," or "."
Example, you are typing a word in a jTextField and then you press the "," or "." button in your keyboard, so the program shows a JOptionPane saying "You cant type , or ."
Is that possible?? Thanks ^^

It's quite possible, and one way is to replace the JTextField's ActionMap associated with those two characters using key bindings. For instance assuming a JTextField called "field":



This doesn't work if you paste text into the JTextField. A DocumentFilter may be what you need if you have to take into consideration pasting of text.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!