How to show different pages from the center element of JFrame (having set to BorderLayout)

import java.awt.BorderLayout;
import java.awt.Component;  
import javax.swing.JFrame;
import javax.swing.border.Border;

public class GuiController extends JFrame {

    private CentreFrameController centreFrameController;
    private CustomerPage customerPage;
    private LoginPage loginPage;
    public GuiController(){
        centreFrameController=new CentreFrameController(this);      
        setLayout(new BorderLayout());      

        add(centreFrameController,BorderLayout.CENTER);     

        setTitle("Courier System-Login Page");
        setVisible(true);
        setSize(550,650);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }   
}

CentreFrameController

import java.awt.BorderLayout;
import java.awt.Graphics; 
import javax.swing.JPanel;

public class CentreFrameController extends JPanel {

    private GuiController guiController;
    private CustomerPage customerPage;
    private LoginPage loginPage;
    public CentreFrameController(GuiController guiController){
        this.guiController=guiController;
        loginPage=new LoginPage(this);
        setLayout(new BorderLayout());

        add(loginPage,BorderLayout.CENTER);
        loginPage.addLoginPageListener(new LoginPageListener(){
            public void getLoginPageReply(int reply) {
                switch(reply){
                case 0:System.out.println("Customer login sucess");
                setCustomerHomePage();
                break;
                case 1:System.out.println("Admin login success");
                }
            }
        });
    }
    public void setCustomerHomePage(){
        customerPage=new CustomerPage(this,loginPage);
        add(customerPage,BorderLayout.CENTER);///This is the part which is not working
    }
}

The aim of this part of the code is:

  1. I want to make the GuiController frame have some common style for my application on all pages (irrespective of any activity)
  2. The GuiController frame gives the center element styling of the BorderLayout to the CentreFrameController panel
  3. Now the CentreFrameController should switch pages after hearing from the LoginListener. But here,when it hears from LoginListener, it's not adding the CustomerPage to its layout (please note that here, the CustomerPage class is made to extend JPanel and I have not uploaded that because its not really necessary).

Solution 1:

Here is a mcve demonstrating how you could use CardLayout as suggested by Andrew Thompson:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GuiController extends JFrame {

    private boolean isRedShowing;

    public GuiController(){

        setTitle("CardLayout Demo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        CentreFrameController centreFrameController = new CentreFrameController();
        centreFrameController.showRedPane();
        isRedShowing = true;
        setLayout(new BorderLayout());
        add(centreFrameController,BorderLayout.CENTER);
        JButton toggle = new JButton("Toggle");
        toggle.addActionListener(e ->{
            if(! isRedShowing) {
                centreFrameController.showRedPane();
            } else {
                centreFrameController.showYelloPane();
            }
            isRedShowing = ! isRedShowing;
        });
        add(toggle,BorderLayout.PAGE_END);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new GuiController();
    }
}

class CentreFrameController extends JPanel {

    public final String YELLOW_PAGE = "yellow page";
    public final String RED_PAGE = "red page";
    private final CardLayout cLayout;

    public CentreFrameController(){

        cLayout = new CardLayout();
        setLayout(cLayout);
        setPreferredSize(new Dimension(200, 150));
        add(YELLOW_PAGE, new YellowPane());
        add(RED_PAGE, new RedPane());
    }

    //two convenience methods that encapsulate CardLayout#show(Container, String)
    void showRedPane() {
        cLayout.show(this, RED_PAGE);
    }

    void showYelloPane() {
        cLayout.show(this, YELLOW_PAGE);
    }
}

class RedPane extends JPanel{

    RedPane(){
        setBackground(Color.RED);
    }
}

class YellowPane extends JPanel{

    YellowPane(){
        setBackground(Color.YELLOW);
    }
}

You control which card shows by using CarrdLayout show.


enter image description here