0

How to make JTextPane to accept only numbers and the following symbols: . , , (dot and coma) in Java?

private void jTextPane6KeyTyped(java.awt.event.KeyEvent evt) {                                            
    char c = evt.getKeyChar();
    if ((Character.isLetter(c) && !(c==KeyEvent.VK_BACKSPACE) || (c==KeyEvent.VK_DELETE))) {
        getToolkit().beep();
        evt.consume();
    }
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
miRM
  • 1
  • 3
  • Why would you use a JTextPane for this? A JTextPane is used to display multiple lines of styled text. In any case you should NOT be using a KeyListener. I suspect you should be using a JFormattedTextField. The other option is a JTextField with a custom DocumentFilter. These solutions are described in the above link. – camickr Dec 03 '16 at 16:16
  • if using jtextformated what I do to declare it? *I'm still a beginner Please help – miRM Dec 06 '16 at 14:06
  • Read the section from the Swing tutorial on [How to Use Formatted Text Fields](http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html) for more information and working examples. Keep a link to the tutorial handy for all the Swing basics. – camickr Dec 06 '16 at 17:54

0 Answers0