adding same object(jtextfield) to different cards in card layout
Have a combo box with 3 items which changes cards in a cardLayout.
Have 3 panels as card in a cardLayout.
Want to reuse the field txtName, lblName in pnlName and reuse txtAge, lblAge in pnlAge in all the cards.
It only appears in the last card3 panel(i.e the last panel referenced)
Is there a way I can show the same object(pnlName, pnlAge) in all the cards,
Below is the code, any solution is welcome.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutTest implements ItemListener {
JPanel cards; //a panel that uses CardLayout
public void addComponentToPane(Container pane) {
//Put the JComboBox in a JPanel to get a nicer look.
JPanel comboBoxPane = new JPanel(); //use FlowLayout
String comboBoxItems[] = { "Name", "ShortDetail","FullDetail"};
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
JLabel lblName, lblAddress, lblAge;
JCheckBox chkMarried;
JRadioButton rdbMale, rdbFemale;
JTextField txtName, txtAddress, txtAge, txtSex;
JPanel pnlName = new JPanel(new FlowLayout(FlowLayout.LEFT));
pnlName.add(lblName = new JLabel("Name"));
pnlName.add(txtName = new JTextField(10));
JPanel pnlAddress = new JPanel(new FlowLayout(FlowLayout.LEFT));
pnlAddress.add(lblAddress = new JLabel("Address"));
pnlAddress.add(txtAddress = new JTextField(10));
JPanel pnlAge = new JPanel(new FlowLayout(FlowLayout.LEFT));
pnlAge.add(lblAge = new JLabel("Age"));
pnlAge.add(txtAge = new JTextField(3));
JPanel pnlSex = new JPanel(new FlowLayout(FlowLayout.LEFT));
pnlSex.add(rdbMale = new JRadioButton("Male"));
pnlSex.add(rdbFemale = new JRadioButton("Female"));
JPanel pnlMarried = new JPanel(new FlowLayout(FlowLayout.LEFT));
pnlMarried.add(chkMarried = new JCheckBox("Married"));
//pnl3.add(lblName = new JTextField(10));
JPanel card1 = new JPanel(new GridLayout(0,1));
card1.add(new JLabel("Card1"));
card1.add(pnlName);
card1.add(pnlAge);
JPanel card2 = new JPanel(new GridLayout(0,1));
card2.add(new JLabel("Card2"));
card2.add(pnlName);
card2.add(pnlAge);
card2.add(pnlAddress);
JPanel card3 = new JPanel(new GridLayout(0,1));
card3.add(new JLabel("Card3"));
card3.add(pnlName);
card3.add(pnlAge);
card3.add(pnlSex);
card3.add(pnlMarried);
/*
//Create the "cards".
JPanel card1 = new JPanel();
card1.add(new JButton("Button 1"));
card1.add(new JButton("Button 2"));
card1.add(new JButton("Button 3"));
JPanel card2 = new JPanel();
card2.add(new JTextField("TextField", 20));
JPanel card3 = new JPanel();
card3.add(new JLabel("Label"));
JPanel card4 = new JPanel();
card4.add(new JCheckBox("Hello Holla"));
JPanel card5 = new JPanel();
cards.add();
*/
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, "Name");
cards.add(card2, "ShortDetail");
cards.add(card3, "FullDetail");
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("CardLayoutTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CardLayoutTest demo = new CardLayoutTest();
demo.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

