Is there any way to accept only numeric values in a JTextField
? Is there any special method for this?
A very easy solution is to use a action listener.
TextFieldActionPerformed(java.awt.event.ActionEvent evt) {
String textFieldValue = TextField.getText();
try {
Integer.parseInteger(textFieldValue);
} catch(Exception e){
JOptionPane.showMessageDialog(null, "Please insert Valid Number Only");
TextField.setText(textFieldValue.substring(0,textFieldValue.length()-1));
}
}
You Can use it for Double as well:
Double.parseDouble(TextField.getText());