Reputation: 363
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;
}
Upvotes: 0
Views: 864
Reputation: 209004
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.
Upvotes: 3