MVC, problems when passing data from a View to a Controller

2

I have a class PaneClient , which uses CardLayout and "above" this view are other 2 views: paneAttributeSet and paneRegisterClient .

The problem is when I try to call the method getFieldData() from the controller PaneRegisterClientController of the view paneRegisterClient by clicking on the Register button of said view, the method must return an object of type Client initialized with the input of the user that would be the ID and name of the client, but the values assigned to the object Client end up being null, it is as if the method could not assign the values inserted in the textfields.

Class PaneClient :

public class PaneClient{

..........botones para CRUD.............
    private JPanel viewClientPane;

    private ClientView clientView = new ClientView();


private AttributesView theAttribView = new AttributesView();

    private ClientAttributeSetManagement paneAttributeSet = new ClientAttributeSetManagement();
    public PaneRegisterClient paneRegisterClient = new PaneRegisterClient();

    public PaneClient() {

        setLayout(new CardLayout());
        initComponents();

    }

    protected void initComponents() {

    MigLayout layout = new MigLayout("wrap 2", "[grow] [] ", "[] [] []");
    viewClientPane = new JPanel(layout);

    // put all the panels together
    viewClientPane.add(clientView, "flowx,alignx left,growy");
    viewClientPane.add(getCrudButtonsPanel(), "cell 0 0");
    viewClientPane.add(theAttribView, "cell 0 1");
    viewClientPane.add(getNavButtonsPanel(), "south");

// añade las views

        add(viewClientPane, "viewClientPane");
        add(paneAttributeSet, "pane Aset");
        add(paneRegisterClient, "pane Register client");

    }
public PaneRegisterClient getPaneRegisterClient() {
    return paneRegisterClient;
}
}

Class PaneRegisterClient :

private JButton registerButton = new JButton("Register");
private JButton backButton = new JButton("Back");
private JButton finishButton = new JButton("Finish");

public JTextField txtClientID = new JTextField(5);
public JTextField txtName = new JTextField(15);

private JPanel registerClientView;

public PaneRegisterClient() {
    setLayout(new CardLayout());
    initComponents();
}

private void initComponents() {

    attributeView = new AttributesView();

    MigLayout layout = new MigLayout("wrap 2", "[grow] [] ", "[] [] []");
    viewClientPane = new JPanel(layout);

    // put all the panels together
    viewClientPane.add(getRegisterUserView(), "flowx,alignx left,growy");
    viewClientPane.add(attributeView, "flowx,cell 0 1");
    viewClientPane.add(registerButton, "cell 0 1");
    viewClientPane.add(getNavButtonsPanel(), "south");

    add(viewClientPane, "viewUserPane");

}

private JPanel getRegisterUserView() {
    registerClientView = getPanel("Client data");
    registerClientView.setLayout(new MigLayout("wrap 2", "[] 16 []"));

    registerClientView.add(new JLabel("Client ID:"), "right");
    registerClientView.add(txtClientID);

    registerClientView.add(new JLabel("Name:"), "right");
    registerClientView.add(txtName);

    return registerClientView;
}
public boolean isEmptyFieldData() {
    return (txtClientID.getText().trim().isEmpty() && txtName.getText().trim().isEmpty());
}

// el metodo llamado que no funziona
    public Client getFieldData() {
        Client client = new Client();

    client.setClientID(txtClientID.getText());
    client.setClientName(txtName.getText());

    System.out.println("method getfieldata: " + client.getClientID() + client.getClientName());

    return client;
}
public void addNavListener(ActionListener navListener) {
    // TODO Auto-generated method stub
    backButton.addActionListener(navListener);
    finishButton.addActionListener(navListener);
}

public void addRegListener(ActionListener regListener) {
    registerButton.addActionListener(regListener);
}

Controller PaneRegisterClientController :

public class PaneRegisterClientController {
private PaneClient paneClient;
private PaneRegisterClient paneRegisterClient;

private ClientBean clientBean = new ClientBean();

public PaneRegisterClientController(PaneClient pClient) {

    this.paneClient = pClient;
    this.paneRegisterClient = pClient.getPaneRegisterClient();

    initComponents();

    paneRegisterClient.addNavListener(new NavListener());
    paneRegisterClient.addRegListener(new RegListener());
}

private void initComponents() {
    // TODO Auto-generated method stub

}

class RegListener implements ActionListener {

    Client client = paneRegisterClient.getFieldData();

    @Override
    public void actionPerformed(ActionEvent e) {

        switch (e.getActionCommand()) {

        case "Register":

            if (paneRegisterClient.isEmptyFieldData()) {
                JOptionPane.showMessageDialog(null, "Some fields are empty");
                return;
            }

            if (clientBean.create(client) != null) {

                JOptionPane.showMessageDialog(null, "New user registered successfully.");

                client.setClientID("");
                client.setClientName("");
                paneRegisterClient.setFieldData(client);
            }
            break;

        }
    }

I suspect that the error is in the way I assign the PaneRegisterClient view to the Controller's tool:

private PaneClient paneClient;
private PaneRegisterClient paneRegisterClient;

public PaneRegisterClientController(PaneClient pClient) {

    this.paneClient = pClient;
    this.paneRegisterClient = pClient.getPaneRegisterClient();
.......

Finally, the question is: what would be a correct way to pass data to the controller using CardLayout and avoid this problem?

Here you can find my whole project

    
asked by Bryan Romero 06.11.2018 в 12:47
source

0 answers