I have a main JFrame which holds a default JPanel. I'm trying to use this JPanel to attach different JPanels to my application to simulate the effect of browsing through the application. I noticed that for CardLayout to work, the JFrame usually has to have some way of controlling what is displayed in the JPanel (ex. a button, drop down box, etc.) What I'm attempting to do is have a home page(JPanel) load up into the default JPanel in the JFrame and allow the user to navigate using the clickable buttons/icons available in that home JPanel.
Since the JFrame and default JPanel are in a separate class from the rest of the JPanels, I'm having trouble accessing the default JPanel (since it is private) to change what appears on it by clicking a button on a JPanel in one of the other classes. Is there any way around this?
Also, would it work if I attached every button necessary in the application to the JFrame and controlled the JPanel displayed from there? I could make the button visible/invisible as necessary, does this make sense?
This is the code I'm calling in the JFrame class:
private void jButton45MouseClicked(java.awt.event.MouseEvent evt) {
CardLayout card= (CardLayout) displayPanel.getLayout();
card.show(displayPanel, "register");
}
displayPanel is the default Panel that cycles through all the cards and jButton45 is a button titled "Register" in the JFrame. "register" is the name of the JPanel variable which I'm trying to display.
show()is not the name of a variable. It is the name that you provide when you calladd(). If you are using the NetBeans GUI Builder, you can set this in the properties window. Select the panel and scroll down to "Layout". Set the Card Name to the desired String value and then use this same value when you callshow(). - Code-Apprentice