Java, if condition for all instance of a class

46 Views Asked by At

I try to code a GUI with multiple textfields. I use the JTextfield command for it. I create several of those textfields, each one being checked by a CaretListener.

Is there a way to automate to check every Textfield for content, by automatically running through all the CaretListeners? I dont want to call every textfield individually, because eventually there will be too many. My code is as follows:

....

//create 4 textfields (Textzuhorer is the internal class for the CaretListener)

    klasse = new JTextField();
    klasse.setBounds(0, 25, 50, 30);
    klasse.setToolTipText("Geben Sie hier die Klasse ein, für die Sie  Stunden festlegen wollen");
    klasse.addCaretListener(new Textzuhorer());
    zeile.add(klasse);


    fach = new JTextField();
    fach.setBounds(60, 25, 80, 30);
    fach.setToolTipText("Geben Sie hier das Fach/die Leiste ein");
    fach.addCaretListener(new Textzuhorer());
    zeile.add(fach);

    stunden = new JTextField();
    stunden.setBounds(150,25,40,30);
    stunden.setToolTipText("Geben Sie hier die Anzahl Stunden pro Woche an");
    stunden.addCaretListener(new Textzuhorer());
    zeile.add(stunden);

    raum = new JTextField();
    raum.setBounds(200, 25, 60, 30);
    raum.setToolTipText("Geben Sie hier die Raumart ein");
    raum.addCaretListener(new Textzuhorer());
    zeile.add(raum);

... instead of calling them one after another, is there any automated process?

    private class Textzuhorer implements CaretListener {

    public void caretUpdate(CaretEvent e) {
        **if (klasse.getText().trim().isEmpty() | fach.getText().trim().isEmpty() | 
            stunden.getText().trim().isEmpty()| raum.getText().trim().isEmpty())** {

            button.setEnabled(false);

        } else {
            button.setEnabled(true);
        }

    }

}

Any advice is highly welcome;)

0

There are 0 best solutions below