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();

}

});

}

}

[4160 byte] By [kdasinca] at [2007-10-3 3:29:44]
# 1

I'm 99% sure that you cannot have the same txtfields/combos in different JPanels at the same time, which explains why they only appear in the last panel they are added to... but I guess you already knew that.

You could add these components to the panel you wish to show each time you change panels.

Or

you could have a panel which contains these components and add it to each of the 3 panels that you show on your cardLayout.

Or

you could store the values selected in the components in globals to your class and replicate each of the components with this data in each of your three panels.

mikeyfreakea at 2007-7-14 21:23:36 > top of Java-index,Desktop,Core GUI APIs...
# 2

Use the "Code" formatting tags when posting code so the code is readable.

As stated above a component can only have a single parent.

However, you can share the model between components. So you can do somthing like:

JTextField textField1 = new JTextField()

JTextField textField2 = new JTextField()

textField2.setDocument( textField1.getDocument() );

For most component the model is called a model and you would use the setModel(...) method. However for text components the model is called a Document. Unfortunately labels don't have a model so you need to recreate them specifically.

camickra at 2007-7-14 21:23:36 > top of Java-index,Desktop,Core GUI APIs...