MVC pattern with CardLayout

145 Views Asked by At

I have a Java Swing application with a single large class that via CardLayout replaces the various JPanels based on the various buttons. Each JPanel is built in its own class. I would like to divide the various JPanels into different Model-View-Controllers. My application has for example a welcome page with a button that goes to the login page, the login page can go to three different pages: forgotten password, registration and access to the personal page. All pages can go back. How could I do?

For the moment I've done something like this:

Model:

public record Model(String name,String surname) {}

MainView:

import javax.swing.*;
import java.awt.*;

public class MainView {
    JFrame frame;
    JPanel cardLayoutPanel;
    JPanel masterPanel;
    CardLayout cl;
    MainView(){
        frame = new JFrame("Frame");
        cardLayoutPanel = new JPanel();
        cardLayoutPanel.setLayout(new CardLayout());
        cl=(CardLayout) cardLayoutPanel.getLayout();

        masterPanel = new JPanel();
        masterPanel.setLayout(new BorderLayout());
        masterPanel.add(cardLayoutPanel);

        frame.getContentPane().add(masterPanel);
        frame.setResizable(true);
        frame.setSize(300, 150);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        MainView viewM= new MainView();
        MainController controllerM=new MainController(viewM);
        SignUpView viewS = new SignUpView();
        SignUpController controllerR=new SignUpController(controllerM,viewS);
        controllerM.addPanel(viewS.panel,"SIGNUP");
        controllerM.showPanel("SIGNUP");
    }
}

MainController:

import javax.swing.*;

public class MainController {
    MainView view;
    MainController(MainView view){
        this.view=view;
    }
    void addPanel(JPanel panel, String name){
        view.cardLayoutPanel.add(panel,name);
    }
    void showPanel(String panel){
        view.cl.show(view.cardLayoutPanel,panel);
    }
}

SignUpView:

import javax.swing.*;

public class SignUpView {
    JPanel panel;
    JTextField name,surname;
    JButton submit;
    SignUpView(){
        panel=new JPanel();
        name=new JTextField(20);
        surname=new JTextField(20);
        submit=new JButton("submit");
        panel.add(name);
        panel.add(surname);
        panel.add(submit);
    }
}

SignUpController:

public class SignUpController{
    SignUpController(MainController main,SignUpView view){
            PersonalAreaView viewPA=new 
PersonalAreaView();
            PersonalAreaController controllerPA = new PersonalAreaController(main,viewPA);
            main.addPanel(viewPA.panel,"PERSONALAREA");
        view.submit.addActionListener(e -> {
            Model model=new Model(view.name.getText(),view.surname.getText());
            controllerPA.setValue(model);
            main.showPanel("PERSONALAREA");
        });
    }
}

PersonalAreaView:

import javax.swing.*;

public class PersonalAreaView{
    JPanel panel;
    JLabel name,surname;
    JButton back;
    PersonalAreaView(){
        panel=new JPanel();
        name=new JLabel();
        surname=new JLabel();
        back=new JButton("Back");
        panel.add(name);
        panel.add(surname);
        panel.add(back);
    }
}

PersonalAreaController:

public class PersonalAreaController{
    PersonalAreaView view;
    PersonalAreaController(MainController main,PersonalAreaView view){
       this.view=view;
       view.back.addActionListener(e -> main.showPanel("SIGNUP"));
    }
   public void setValue(Model model){
   view.name.setText(model.name());
   view.surname.setText(model.surname());
   }
}

The code can be found on GitHub: GitHub code

Is something correct or am I wrong?

0

There are 0 best solutions below