Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your provided code, you never added <code>FirstCard</code> and <code>SecondCard</code>, to the <code>JPanel</code> having layout set to <code>CardLayout</code>. Since what you writing is this : </p> <pre><code>jPanel1.add(jPanel2, "card2"); </code></pre> <p>here <code>jPanel2</code> is an instance of <code>JPanel</code>, as you have initialized this in your <code>TestmyClass</code> Class, as :</p> <pre><code>jPanel2 = new javax.swing.JPanel(); </code></pre> <p>instead I guess what you should be writing is :</p> <pre><code>jPanel2 = new SecondCard(passPanelWithCardLayoutAsArgument); // So that you can manoeuvre around b/w other JPanels </code></pre> <p>Here is a small working example for your help : </p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CardLayoutExample { private JPanel contentPane; private MyPanel panel1; private MyPanel panel2; private MyPanel panel3; private void displayGUI() { JFrame frame = new JFrame("Card Layout Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new CardLayout()); panel1 = new MyPanel(contentPane , Color.RED.darker().darker()); panel2 = new MyPanel(contentPane , Color.GREEN.darker().darker()); panel3 = new MyPanel(contentPane , Color.DARK_GRAY); contentPane.add(panel1, "Panel 1"); contentPane.add(panel2, "Panel 2"); contentPane.add(panel3, "Panel 3"); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new CardLayoutExample().displayGUI(); } }); } } class MyPanel extends JPanel { private JButton jcomp1; private JPanel contentPane; private Color backgroundColour; public MyPanel(JPanel panel, Color c) { contentPane = panel; backgroundColour = c; setOpaque(true); setBackground(backgroundColour); //construct components jcomp1 = new JButton ("Show New Panel"); jcomp1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { CardLayout cardLayout = (CardLayout) contentPane.getLayout(); cardLayout.next(contentPane); } }); add(jcomp1); } @Override public Dimension getPreferredSize() { return (new Dimension(500, 500)); } } </code></pre> <p><strong>LATEST EDIT :</strong> *<strong>Using your components and trying to put that into CardLayout, with this code : *</strong></p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CardLayoutExample { private JPanel contentPane; private FirstCard panel1; private SecondCard panel2; private void displayGUI() { JFrame frame = new JFrame("Card Layout Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new CardLayout()); panel1 = new FirstCard(contentPane); panel2 = new SecondCard(contentPane); contentPane.add(panel1, "Panel 1"); contentPane.add(panel2, "Panel 2"); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new CardLayoutExample().displayGUI(); } }); } } class FirstCard extends javax.swing.JPanel { private javax.swing.JTextField addField; private javax.swing.JTextField nameField; private javax.swing.JTextField occField; private javax.swing.JTextField phoneField; private javax.swing.JLabel nameLabel; private javax.swing.JLabel addLabel; private javax.swing.JLabel occLabel; private javax.swing.JLabel phoneLabel; private JPanel centerPanel; private JPanel contentPane; private JButton nextButton; public FirstCard(JPanel cp) { this.contentPane = cp; initComponents(); } private void initComponents() { setOpaque(true); setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); setBackground(Color.RED); setLayout(new BorderLayout(5, 5)); nameLabel = new javax.swing.JLabel("Guarantee Name : "); nameField = new javax.swing.JTextField(); addLabel = new javax.swing.JLabel("Address : "); addField = new javax.swing.JTextField(); occLabel = new javax.swing.JLabel("Occupation : "); occField = new javax.swing.JTextField(); phoneLabel = new javax.swing.JLabel("Phone : "); phoneField = new javax.swing.JTextField(); centerPanel = new JPanel(); nextButton = new JButton("Next"); nextButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { nextButtonAction(ae); } }); centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); centerPanel.setOpaque(true); centerPanel.setBackground(Color.WHITE); centerPanel.setLayout(new GridLayout(0, 2, 5, 5)); centerPanel.add(nameLabel); centerPanel.add(nameField); centerPanel.add(addLabel); centerPanel.add(addField); centerPanel.add(occLabel); centerPanel.add(occField); centerPanel.add(phoneLabel); centerPanel.add(phoneField); add(centerPanel, BorderLayout.CENTER); add(nextButton, BorderLayout.PAGE_END); } private void nextButtonAction(ActionEvent ae) { CardLayout layout = (CardLayout)contentPane.getLayout(); layout.next(contentPane); } } class SecondCard extends javax.swing.JPanel { private javax.swing.JButton nextButton; private javax.swing.JLabel textLabel; private JPanel contentPane; public SecondCard(JPanel cp) { contentPane = cp; initComponents(); } private void initComponents() { setOpaque(true); setBackground(Color.GREEN.darker().darker()); setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); textLabel = new JLabel("this is the second card", JLabel.CENTER); textLabel.setForeground(Color.WHITE); nextButton = new javax.swing.JButton(); nextButton.setText("SwitchCard"); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); } }); add(textLabel); add(nextButton); } private void jButton1ActionPerformed(ActionEvent evt) { CardLayout layout = (CardLayout) contentPane.getLayout(); layout.show(contentPane, "Panel 1"); } } </code></pre> <p><strong>Method to clear fields</strong></p> <pre><code>private void clearFields() { Component components[] = centerPanel.getComponents(); for (Component comp : components) { if (comp instanceof JTextField) { JTextField tfield = (JTextField) comp; tfield.setText(""); } else if (comp instanceof JComboBox) { JComboBox cbox = (JComboBox) comp; cbox.setSelectedIndex(0); } else if (comp instanceof JRadioButton) { JRadioButton rbut = (JRadioButton) comp; rbut.setSelected(false); } } } </code></pre> <p>And you will call this inside the <code>actionPerformed()</code> method of the Button, which will take you to the next Card.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload