This discussion is archived
6 Replies Latest reply: Sep 2, 2012 11:01 PM by StanislavL RSS

how to enable JButton until JTextFields are not empty

958571 Newbie
Currently Being Moderated
Please, l need help on how to code situation that all JTextFields in JFrame is filled before the save JButton is enable. A kind of validation which make sure that use input values into JTextFields. Thanks
  • 1. Re: how to enable JButton until JTextFields are not empty
    Jörg Explorer
    Currently Being Moderated
    I think it's more common to do that validation after the user presses the Save button - at least if there are quite a number of textfields in the form. The checking routine could then also tell the user which field(s) are missing input.
    If, however, there are only let's say two fields in the form, then you could attach a focusListener or inputVerifier to each field and check both fields on focusLost.
  • 2. Re: how to enable JButton until JTextFields are not empty
    StanislavL Pro
    Currently Being Moderated
    Add a DocumentListener to all the fields. On each insert or remove call run checkEmpty() method. In the method iterate through list of the JTextFields and fill one boolean flag isAllFieldsFilled. Depending on the flag enable/disable the button.
  • 3. Re: how to enable JButton until JTextFields are not empty
    958571 Newbie
    Currently Being Moderated
    Thanks StanislavL for finding out time to respond. I tried addDocumentListener on all the field but anytime l type a single character it will enable the save button and that is not what l want. l want a situation where all the field is filled first before the save button is enable and if by any chance a field is empty by a user the save but will go back to disable mode. l will be glad if you can show me some code that will do that. Thanks
  • 4. Re: how to enable JButton until JTextFields are not empty
    StanislavL Pro
    Currently Being Moderated
    Then your check doesn't work correctly. In the check method you should ask all the fields and enable the button if all of them are filled.
    l will be glad if you can show me some code that will do that
    I will be glad to help if you post your SSCCE to show us your code and then we could tell you what's incorrect there.
  • 5. Re: how to enable JButton until JTextFields are not empty
    958571 Newbie
    Currently Being Moderated
    Thanks Stanislav, l was able to done it. The problem was really in checkEmpty method, l was setting focus on the textfield, immediately commented the line that setfocus and called in it in the insertUpdate() and removeUpdate() methods of DocumentListener it works. Below is the code. But if you have a better way of doing it l will appreciate and l also want to check and ensure that textfield accept string only, how do l do it?

    boolean isTextFieldEmpty() {
    Component[] component = dialogView.getControls();
    for (Component ctrl : component) {
    if (ctrl instanceof JTextField) {
    JTextField jf = (JTextField) ctrl;
    if (jf.getText().equals("")) {
    //jf.requestFocus();
    return true;
    }
    }
    }
    return false;
    }

    class DialogViewDocumentListener implements DocumentListener {

    DialogView dialogView;
    Controller controller;

    public DialogViewDocumentListener(DialogView dialogView, Controller controller) {
    this.dialogView = dialogView;
    this.controller = controller;
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
    if (!controller.isTextFieldEmpty()) {
    dialogView.enableButton();
    }
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
    if (controller.isTextFieldEmpty()) {
    dialogView.disEnableButton();
    }
    }
    }
  • 6. Re: how to enable JButton until JTextFields are not empty
    StanislavL Pro
    Currently Being Moderated
    The code is ok for me. The only thing I would suggest is to envelope the check in SwingUtilities.invokeLater.

    If you think my answer is correct just mark it as correcct (or helpful) to let another readers see which solutions work.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points