2

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Nice written question (well formatted, clearly formulated). Did you register the `DocumentListener` somewhere, so it can have effect? See [similar question](https://stackoverflow.com/questions/1625855/how-to-disable-javax-swing-jbutton-in-java) – hc_dev Apr 07 '20 at 19:49
  • Hi I have just tried that the implementation in the code works but when opening the GUI the buttons still work eventhough the textfields are empty the code for the register looks like this ` add(terrain); terrain.setEditable(true); terrain.getDocument().addDocumentListener(this); ` – Philip Inderthal Apr 08 '20 at 20:45

1 Answers1

1

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
}