public
Created

CardLayout Helper

  • Download Gist
CardLayoutHelper.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.