how to filter certain characters in JTextField

时光毁灭记忆、已成空白 提交于 2019-12-03 18:16:35

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/15703644/how-to-filter-certain-characters-in-jtextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!