Created
December 5, 2012 09:02
-
-
Save dulichan/4214039 to your computer and use it in GitHub Desktop.
CardLayout Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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