Here is a small program I tossed together real quick. It should run perfectly. I added some comments so you can understand what is happening. As you will see, it is very simple. If you have any questions, feel free to ask.
You can selected the Card using the JList on the left-hand side, or by using the Next and Previous buttons at the top.
/**
* @(#)CardLayoutExample.java
*
*
* @author
* @version 1.00 2012/5/14
*/
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class CardLayoutExample extends JFrame {
JList menuOptions;
JButton next;
JButton previous;
Vector<ContentPanel> panels;
//Create Card Holder Panel as global variable so you can use it inside listeners
JPanel cardHolder;
public CardLayoutExample() {
super("Example");
setSize(500,500);
panels = new Vector<ContentPanel>();
//Create x number of ContentPanels
int number = 5;
for(int i=0;i<number;i++) {
panels.add(new ContentPanel(i+1));
}
//Initialize Card Holder and send it a new CardLayout() as the constructor. Set the Preferred Size, and set the Border for clarity
cardHolder = new JPanel(new CardLayout());
cardHolder.setPreferredSize(new Dimension(200,200));
cardHolder.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
//Initialize button that moves to the next Card
next = new JButton("Next");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Call the next card method
((CardLayout)cardHolder.getLayout()).next(cardHolder);
}
});
//Initialize button that moves to the previous Card
previous = new JButton("Previous");
previous.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Call the next card method
((CardLayout)cardHolder.getLayout()).previous(cardHolder);
}
});
//Create Menu List that allows user to select the Card, set the size of the Menu List, and add a List Selection Listener
menuOptions = new JList(panels);
menuOptions.setPreferredSize(new Dimension(50,200));
menuOptions.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e)
{
//Return if Event is adjusting
if(e.getValueIsAdjusting())
return;
//Get the selected index and change Cards
int selectedIndex = menuOptions.getSelectedIndex();
((CardLayout)cardHolder.getLayout()).show(cardHolder,menuOptions.getSelectedValue().toString());
}
});
//Add all the Cards to the Card Holder
for(ContentPanel panel : panels) {
cardHolder.add(panel,panel.toString());
}
//Panel that holds the Next and Previous Buttons
JPanel top = new JPanel();
top.setPreferredSize(new Dimension(200,50));
top.add(next);
top.add(previous);
//Add Buttons, the Menu, and the Cards to the JFrame
getContentPane().add(top,BorderLayout.PAGE_START);
getContentPane().add(menuOptions,BorderLayout.LINE_START);
getContentPane().add(cardHolder,BorderLayout.CENTER);
setVisible(true);
}
public class ContentPanel extends JPanel {
JLabel label;
int num;
public ContentPanel(int n) {
super();
setPreferredSize(new Dimension(200,200));
num = n;
label = new JLabel("This is Panel "+n);
add(label);
}
public String toString() {
return "Panel "+num;
}
}
public static void main(String[] args) {
new CardLayoutExample();
}
}