Is there any way to accept only numeric values in a JTextField?

前端 未结 19 1888
陌清茗
陌清茗 2020-11-22 03:10

Is there any way to accept only numeric values in a JTextField? Is there any special method for this?

19条回答
  •  不思量自难忘°
    2020-11-22 03:51

    if (JTextField.getText().equals("") || !(Pattern.matches("^[0-9]+$", JTextField.getText()))) {
         JOptionPane.showMessageDialog(null, " JTextField Invalide !!!!! ");
       }
    
    • if JTextField.getText().equals("") ==-> if JTextField is empty
    • if(!(Pattern.matches("^[0-9]+$", JTextField.getText()))) ==-> if TextField contains other characters than those
    • JOptionPane.showMessageDialog(null, " JTextField Invalide !!!!! ");==-> so this message will aparate

提交回复
热议问题