简体   繁体   中英

how to filter certain characters in JTextField

如何防止用户在“ JTextField”中输入某些字符,并且如果输入了该字符,则不要在文本字段中显示该字符

您可以使用JFormattedTextField或创建自定义DocumentFilter

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM