Java – Check if JTextField is empty or not

For that you need to add change listener (a DocumentListener which reacts for change in the text) for your JTextField, and within actionPerformed(), you need to update the loginButton to enabled/disabled depending on the whether the JTextfield is empty or not. Below is what I found from this thread. yourJTextField.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent … Read more

DocumentListener Java, How do I prevent empty string in JTextBox?

As @HFOE suggests, InputVerifier is the right choice, but verify() “should have no side effects.” Instead, invoke calcProduct() in shouldYieldFocus(). /** * @see http://stackoverflow.com/a/11818946/230513 */ private class MyInputVerifier extends InputVerifier { private JTextField field; private double value; public MyInputVerifier(JTextField field) { this.field = field; } @Override public boolean shouldYieldFocus(JComponent input) { if (verify(input)) { field.setText(String.valueOf(value)); … Read more

java change the document in DocumentListener

DocumentListener is really only good for notification of changes and should never be used to modify a text field/document. Instead, use a DocumentFilter Check here for examples FYI The root course of your problem is that the DocumentListener is notified WHILE the document is been updated. Attempts to modify the document (apart from risking a … Read more

Value Change Listener to JTextField

Add a listener to the underlying Document, which is automatically created for you. // Listen for changes in the text textField.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { warn(); } public void removeUpdate(DocumentEvent e) { warn(); } public void insertUpdate(DocumentEvent e) { warn(); } public void warn() { if (Integer.parseInt(textField.getText())<=0){ JOptionPane.showMessageDialog(null, “Error: Please enter number … Read more