0

I have a JFrame that contains a jpanel with three butons. I desire that when I clic a button in the child a event is fired into parent parent. I desire to add a Jcardlayout and a button in Jpanel is clicked, the value of jcardlayout changes.

public class Project{

private static JPanel pane = null, p = null;
private static CardLayout card = null;

public static void main(String[] args) {
    // TODO code application logic here
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createFrame();
        }
    });
}

private static void createFrame() {


    JFrame jf = new JFrame("Principal Frame");
    p = new JpMenu();
    jf.getContentPane().add(pane, BorderLayout.CENTER);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setSize(new Dimension(300, 200));
    jf.setVisible(true);
    jf.setResizable(false);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    jf.setExtendedState(JFrame.MAXIMIZED_BOTH);


}

}

And the Jpanel generated for netbeans

public class JpMenuPrincipal extends javax.swing.JPanel  {

public JpMenuPrincipal() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    btnOptionClient = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();

    btnOptionClient.setText("jButton1");
    btnOptionClient.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnOptionClientActionPerformed(evt);
        }
    });

    jButton2.setText("jButton2");

    jButton3.setText("jButton3");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(19, 19, 19)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jButton3)
                .addComponent(jButton2)
                .addComponent(btnOptionClient))
            .addContainerGap(32, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(btnOptionClient)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButton2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButton3)
            .addContainerGap(245, Short.MAX_VALUE))
    );
}// </editor-fold>   
 private void btnOptionClientActionPerformed(java.awt.event.ActionEvent evt) {                                                

}                                               

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
}                        




private javax.swing.JButton btnOptionClient;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
}
Francesc Clopes
  • 353
  • 1
  • 7
  • 19
  • possible duplicate of [Java/Swing: Obtain Window/JFrame from inside a JPanel](http://stackoverflow.com/questions/9650874/java-swing-obtain-window-jframe-from-inside-a-jpanel) – Chris Jan 26 '14 at 21:20
  • Where is the `btnOptionClientActionPerformed` method? – Lord M-Cube Jan 26 '14 at 21:23
  • Code edited, added the actionListener of the butons. – Francesc Clopes Jan 26 '14 at 21:28
  • 1) get rid of all of your static variables. 2) You will want to create and post a [compilable, runnable, minimal example program](http://stackoverflow.com/help/mcve) to give us the best chance of fully understanding your problem. – Hovercraft Full Of Eels Jan 26 '14 at 21:32

1 Answers1

3

Pass the Project to the constructor of JpMenuPrincipal and have a getter for the CardLayout in the Project class.

private Project project;

public JpMenuPrincipal(final Project project) {
    initComponents();
    this.project = project;
}

private void btnOptionClientActionPerformed(java.awt.event.ActionEvent evt) {                                                
    CardLayout card = project.getCardLayout();
    // do something with card
}

Also note you added jpMenu and not JpMenuPrincipal to the frame.

Also as @HovercraftFullOfEels noted, you don't need to make the fields static in Project. Just use getters for the fields you want to access.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 1
    Personally, I prefer to pass some kind of `Interface` as it doesn't expose the parent component unnecessarly, but +1 for the general approach – MadProgrammer Jan 26 '14 at 22:03
  • @MadProgrammer makes total sense. That will be my approach in answers from now on. Learn something new everyday :) – Paul Samsotha Jan 26 '14 at 22:06
  • I create a public method into jFrame parent that changes the value of card layout and the actions of butons call this method. Thanks. – Francesc Clopes Jan 26 '14 at 22:15