Validating a textfield is empty of not can be done by getting text from the textview and comparing it to ""
Suppose your TextField is textField.
if (textField.getText().trim().length>0) {
//TextField is empty
} else {
//TextField is not empty
}
Similarly if you want to see a 10 digit contact number.
if (textfield.getText().length == 10) {
/*
Here I'm not checking whether each character is a digit,
but you can do so by iterating through each character and checking
whether it's a digit using isDigit() method
*/
} else {
//Not 10 characters
}
The appropriate listener in Java's swing to track changes in the text content of a JTextField is a DocumentListener, that you have to add to the document of the JTextField:
textField.getDocument().addDocumentListener(new DocumentListener() {
// Enable the buttons here.
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…