Use setEnabled to enable button if specific textfields are filled

Viewed 34

I have a project in which i have to enable and disable the use of buttons (addRn, addSw, and addCy) with setEnabled(). I have tried many things including adding a documentListener but I am getting too confused. Does anyone have an idea what to do?

  ArrayList<JTextField> run = new ArrayList<>();
        run.add(intervals);
        run.add(minRest);

        ArrayList<JTextField> swim = new ArrayList<>();
        swim.add(intervals);
        swim.add(minRest);
        swim.add(loc);

        ArrayList<JTextField> cycle = new ArrayList<>();
        cycle.add(tempo);
        cycle.add(terrain);

        DocumentListener listener = new DocumentListener() {
            @Override
            public void removeUpdate(DocumentEvent e) {
                changedUpdate(e);
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                changedUpdate(e);

            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                boolean canEnable = true;
                for (JTextField intervals : run) {
                    intervals.getDocument().addDocumentListener(listener);
                    if (intervals.getText().isEmpty()) {
                        canEnable = false;
                    }
                }
                for (JTextField minRest : run) {
                    if (minRest.getText().isEmpty()) {
                        canEnable = false;
                    }
                }
                addRn.setEnabled(canEnable);
            }
        };

this is what I have so far, but i feel like there is quite a bit missing and I am not familiar enough to find the problem. I tried to approach it as was suggested here

1 Answers

UPDATE I managed to figure it out

public class TrainingRecordGUI extends JFrame implements ActionListener, DocumentListener {

add DocumentListener to public class

then register the DocumentListener with the textfields that you want to use

public TrainingRecordGUI() {
 add(intervals);
        intervals.setEditable(true);
        intervals.getDocument().addDocumentListener(this); //addDocumentListener for button enabling
 add(labminr);
        add(minRest);
        minRest.setEditable(true);
        minRest.getDocument().addDocumentListener(this);//addDocumentListener for button enabling
        add(labloc);
        add(loc);
        loc.setEditable(true);
        loc.getDocument().addDocumentListener(this);//addDocumentListener for button enabling
        add(labter);
        add(terrain);
        terrain.setEditable(true);
        terrain.getDocument().addDocumentListener(this);//addDocumentListener for button enabling
        add(labtempo);
        add(tempo);
        tempo.setEditable(true);
        tempo.getDocument().addDocumentListener(this);//addDocumentListener for button enabling
}

then add final code for the cases where the button should be enabled and when it shouldn't

// Button enabling/disabling code

@Override
public void insertUpdate(DocumentEvent e) {
    changedUpdate(e);

}

@Override
public void removeUpdate(DocumentEvent e) {
    changedUpdate(e);

}

@Override
public void changedUpdate(DocumentEvent e) {
    //boolean variables to set run, swim and cycle button enabled
    boolean canEnableRn = false;
    boolean canEnableSw = false;
    boolean canEnableCy = false;

    //if anything but necessary fields for run filled
       if (intervals.getText().equals("") && minRest.getText().equals("") 
               || !intervals.getText().equals("") && minRest.getText().equals("") 
               || intervals.getText().equals("") && !minRest.getText().equals("")
               || !loc.getText().equals("") && !terrain.getText().contentEquals("") && !tempo.getText().equals("")) {
           canEnableRn = false; // set canEnableRn false to prevent the button to be enabled
       }else { canEnableRn = true;} //else set true and allow user to add to run
       addRn.setEnabled(canEnableRn); // set button to boolean value
}
Related