Search code examples
javaswingvariablesjpanelcardlayout

How do I pass values between cards in Java CardLayout....this time i have given a good example


This is a simple code to clarify my question:

this is the main class from where I declared the two panels. /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

/** * * @author sakib */ public class MainPane {

private JPanel contentPane;
private Firstcard1 p1;
private SecondCard1 p2;




public void displayGUI()
{
    JFrame frame = new JFrame("Card Layout Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel contentPane = new JPanel();
    //a=S1;

    contentPane.setLayout(new CardLayout());

    p1 = new Firstcard1(contentPane);
    p2 = new SecondCard1(contentPane);

    contentPane.add(p1, "Panel 1");
    contentPane.add(p2, "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 MainPane().displayGUI();
        }
    }

    );   

}

}

this is the first card:

public class Firstcard1 extends javax.swing.JPanel {

private JPanel contentpane;
/**
 * Creates new form Firstcard1
 */
public Firstcard1(JPanel cp) {
    this.contentpane=cp;
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jTextField1 = new javax.swing.JTextField();
    Login = new javax.swing.JButton();

    Login.setText("Login");
    Login.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            LoginActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(117, 117, 117)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(158, 158, 158)
                    .addComponent(Login)))
            .addContainerGap(145, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(105, 105, 105)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(Login)
            .addContainerGap(134, Short.MAX_VALUE))
    );
}// </editor-fold>                        

private void LoginActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    CardLayout layout = (CardLayout)contentpane.getLayout();
    layout.show(contentpane, "Panel 2");
}                                     


// Variables declaration - do not modify                     
private javax.swing.JButton Login;
private javax.swing.JTextField jTextField1;
// End of variables declaration                   

}

this is the second card:

public class SecondCard1 extends javax.swing.JPanel {

private JPanel contentpane;
/**
 * Creates new form SecondCard1
 */
public SecondCard1(JPanel cp) {
    this.contentpane=cp;
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jTextField1 = new javax.swing.JTextField();
    back = new javax.swing.JButton();

    back.setText("Back");
    back.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            backActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(127, 127, 127)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(157, 157, 157)
                    .addComponent(back)))
            .addContainerGap(137, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(121, 121, 121)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(back)
            .addContainerGap(102, Short.MAX_VALUE))
    );
}// </editor-fold>                        

private void backActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
     CardLayout layout = (CardLayout)contentpane.getLayout();
    layout.show(contentpane, "Panel 1");
}                                    


// Variables declaration - do not modify                     
private javax.swing.JButton back;
private javax.swing.JTextField jTextField1;
// End of variables declaration                   

}

My question is.if I put a value in the textarea of the first card and press the login button.the value will appear in the text area of the second card.how can i do it?


Solution

  • You somehow have to pass a reference of the second panel to the first one:

    Main class:

    public class MainPane 
    {
        public void displayGUI()
        {
            ....
            p1 = new Firstcard1(contentPane);
            p2 = new SecondCard1(contentPane);
    
            p1.setSecondCard(p2);
            ....
        }
    }
    

    First card:

    public class Firstcard1 extends javax.swing.JPanel {
        ....
    
        private SecondCard secondCard;
    
        void setSecondCard(SecondCard secondCard)
        { 
            this.secondCard = secondCard;
        }
    
        ....
    
        private void LoginActionPerformed(java.awt.event.ActionEvent evt)
        {                                      
            CardLayout layout = (CardLayout)contentpane.getLayout();
            layout.show(contentpane, "Panel 2");
            secondCard.getTextField().setText(jTextField1.getText());
        }                                 
    }
    

    Second card:

    public class SecondCard1 extends javax.swing.JPanel 
    {
        ...
        JTextField getTextField()
        {
            return jTextField1;
        }
        ...
    }
    

    But when you continue like this, the program will probably become a mess pretty soon. I'd recommend to create these components manually, without using any visual GUI builders.