Reputation: 336
I'm trying to make the JPanels swap from one to another. Sounds simple, but I'm struggling due to having my panels all in different classes.
I have 4 classes: - GUI (Main class) - BankingAppMainFrame (JFrame class) - BankingAppStartPanel (JPanel class) <- I want to remove this JPanel - BankingAppLoginPanel (JPanel class) <- I want to add this JPanel
Here is my relevant JFrame code:
//This is the only way i didn't get errors xD
public void panelSwap(JFrame z, JPanel x, JPanel y) {
z.remove(x);
z.add(y);
}
Here is my relevant BankingAppStartPanel code:
LoginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
//I thought I'd need to instance all my classes
BankingAppMainFrame mainFrame = new BankingAppMainFrame();
BankingAppStartPanel startPanel = new BankingAppStartPanel();
BankingAppLoginPanel loginPanel = new BankingAppLoginPanel();
//This is calling the panelSwap method from the main Class
mainFrame.panelSwap(mainFrame, startPanel, loginPanel);
}
});
The BankingAppLoginPanel is just a blank panel at the moment until I can get this to work.
What I'm trying to do is remove the current Panel (In this case "BankingAppStartPanel" and replace it with the "BankingAppLoginPanel".
Upvotes: 1
Views: 296
Reputation: 109823
//This is the only way i didn't get errors xD
public void panelSwap(JFrame z, JPanel x, JPanel y) {
z.remove(x);
z.add(y);
}
LayoutManager hasn't any notifier implemented in API(s) that notify about changes in visible Swing GUI, you have to notify programatically by calling z.revalidate()
and z.repaint()
there is shortcuts by calling JFrame.pack()
in the cases when you need to change JFrames bounds too
JFrame.pack()
in the cases when you need to change JFrame
s boundsUpvotes: 3
Reputation: 1379
If you planning to go ahead with this "swapping" panels approach, you better try using Card Layout
Upvotes: 3