java – How should I access the content of a JTextField?

Question:

In a java project without Database connection where we only have the Model, View and Controller layers, what would be the most adequate way to get a String that is contained in a JTextField of a class? For example, we have a text field on the View layer:

JTextField txt = new JTextField("Teste");

Suppose I want to get this text in the Controller layer, one of the alternatives would be to make the txt a public object, or generate the following method in the Controller class:

public String getTxt() {
        return txt.getText();
    }

I'm in doubt which of the two ways to use, since the first one brings some vulnerability to the application's encapsulation, and the second way seems to me a bit "ugly" to do it. What would be the most correct way to do this?

Answer:

Perhaps it's best to use an event model. For example:

public class MeuEvento {
    private final String nomeInformado;

    public MeuEvento(String nomeInformado) {
        this.nomeInformado = nomeInformado;
    }

    public String getNomeInformado() {
        return nomeInformado;
    }
}
public class MinhaView {
    private final JFrame frame;
    private final JTextField nomeInformado;
    private final JButton botaoOk;
    private final List<Consumer<MeuEvento>> listeners;
    // ...

    public MinhaView() {
        this.frame = new JFrame();
        this.txt = new JTextField("Teste");
        this.listeners = new ArrayList<>();
        this.botaoOk = new JTextButton("Ok");

        // blablabla...

        this.botaoOk.addActionListener(evt -> {
            MeuEvento me = new MeuEvento(txt.getText());
            for (Consumer<MeuEvento> e : listeners) {
                e.accept(me);
            }
        });
    }

    public void addMeuEventoListener(Consumer<MeuEvento> listener) {
        listeners.add(listener);
    }

    public void removeMeuEventoListener(Consumer<MeuEvento> listener) {
        listeners.remove(listener);
    }
}
public interface MeuModel {

    public void nomeInformado(String nome);

    // Mais um monte de outros métodos...

}
public class MeuController {

     public MeuController(MeuModel model) {
         MinhaView v = new MinhaView();
         v.addMeuEventoListener(m -> model.nomeInformado(m.getNomeInformado()));
     }
}

When using this format, the events triggered by the view are directly sent to the model , the controller being only responsible for translating/adapting them. The events themselves serve to pass data between the view and the controller (but it could also be directly between the view and the model , with the downside that the model would receive events instead of just responding to normal method calls).

If there are several possible actions that the screen can perform reflecting different actions in the model (not necessarily related 1-to-1), there will be an event class for each one of them, so that each event will carry the data that are convenient/necessary.

Scroll to Top