Skip to content

Instantly share code, notes, and snippets.

@dulichan
Created December 5, 2012 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dulichan/4214039 to your computer and use it in GitHub Desktop.
Save dulichan/4214039 to your computer and use it in GitHub Desktop.
CardLayout Helper
package org.dchan.ui;
import java.awt.CardLayout;
import javax.swing.JPanel;
/**
*
* @author Dchan(Dulitha Wijewantha)
*
* This class is used to switch Cards in a CardLayout. I am removing all cards since this is a sample
* code. You can refer the cards in a map for better performance.
*
* @version $Revision: 1.0 $
*/
public class CardLayoutHelper {
private JPanel panel;
private CardLayout layout;
/**
*
*
* @param panel
* JPanel
*/
public CardLayoutHelper(JPanel panel) {
this.panel = panel;
this.layout = (CardLayout) this.panel.getLayout();
}
public CardLayoutHelper(JPanel panel, JPanel... panels) {
this(panel);
for (int i = 0; i < panels.length; i++) {
JPanel jPanel = panels[i];
panel.add(jPanel.getName(), jPanel);
}
}
/**
*
* @param currentPanel
* - The panel that will be switched into the view
*/
public void switchPanel(JPanel currentPanel) {
panel.removeAll();
panel.add(currentPanel, currentPanel.getName());
layout.show(panel, currentPanel.getName());
panel.revalidate();
panel.repaint();
}
public void switchPanel(String name) {
layout.show(panel, name);
panel.revalidate();
panel.repaint();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment