how to filter certain characters in JTextField

后端 未结 2 2135
生来不讨喜
生来不讨喜 2020-12-22 01:42

How to prevent user from entering certain charcters in \'JTextField\' and if enters that character is entered ,do not show it in the textfield

相关标签:
2条回答
  • 2020-12-22 02:25

    You can either use a JFormattedTextField or create a custom DocumentFilter.

    0 讨论(0)
  • 2020-12-22 02:41
    JTextField textField = new JTextField(10);
    textField.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
      char c = e.getKeyChar();
      if (//Write your condition here) {
         e.consume();  // ignore event
    }});
    

    More on the same here

    0 讨论(0)
提交回复
热议问题