I have a main JPanel target in MainFraim and another currentView JPanel, which is added to the target panel. target panel contains buttons with listeners. These listeners are then supposed the change the content of the curretView panel, as demonstrated below:
private JPanel currentPanel;
public void setView(String type) {
if (type.equals("overall")) {
this.currentPanel = getOverallView();
frame.setTitle("BookingCalendar - Overall View");
frame.validate();
} else if (type.equals("guest")) {
this.currentPanel = getGuestView();
frame.setTitle("BookingCalendar - Room View");
frame.validate();
} else if (type.equals("room")) {
currentPanel.removeAll();
this.currentPanel = getRoomView();
frame.setTitle("BookingCalendar - Guest View");
frame.validate();
}
}
Every method I call returns new JPanel each time it is called:
JPanel currentPanel = new JPanel(new MigLayout("","20 [grow, fill] 10 [grow, fill] 20", "20 [] 10 [] 20"))
The problem is, whenever I call these methods, the panel won’t change. It always stays the same (by default: getOverallView() )
I’ve tried with invalidate, validate, repaint on both frame, as well as the panel, but no changes occur. Could someone please elaborate more as to what I need to do in order to completely change the content of the panel currentView
You shouldn’t be creating a new JPanel() every time. Instead create one for each type of view, and use a CardLayout to switch between them. CardLayout lets you have JPanels that get switched out like cards.