Reputation: 1
I am creating a menu swing application, I used a splitpane and the right component to import various jpanel. Once I click the home panel buttion, the other jpanel are not loading if I press the coressponding buttion
import javax.swing.JOptionPane;
/**
*
* @author
*/
public class Mainframe extends javax.swing.JFrame {
/**
* Creates new form Mainframe
*/
int n=0;
public Mainframe() {
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() {
jButton2 = new javax.swing.JButton();
SplitPane = new javax.swing.JSplitPane();
jPanel2 = new javax.swing.JPanel();
jButton3 = new javax.swing.JButton();
workpanel = new javax.swing.JPanel();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton2.setText("Home");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton3.setText("Back");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton3)
.addGap(0, 28, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(391, Short.MAX_VALUE)
.addComponent(jButton3)
.addGap(225, 225, 225))
);
SplitPane.setLeftComponent(jPanel2);
jButton4.setText("User");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("Admin");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
javax.swing.GroupLayout workpanelLayout = new javax.swing.GroupLayout(workpanel);
workpanel.setLayout(workpanelLayout);
workpanelLayout.setHorizontalGroup(
workpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(workpanelLayout.createSequentialGroup()
.addContainerGap(403, Short.MAX_VALUE)
.addGroup(workpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, workpanelLayout.createSequentialGroup()
.addComponent(jButton4)
.addGap(274, 274, 274))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, workpanelLayout.createSequentialGroup()
.addComponent(jButton5)
.addGap(259, 259, 259))))
);
workpanelLayout.setVerticalGroup(
workpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(workpanelLayout.createSequentialGroup()
.addGap(245, 245, 245)
.addComponent(jButton4)
.addGap(112, 112, 112)
.addComponent(jButton5)
.addContainerGap(237, Short.MAX_VALUE))
);
SplitPane.setRightComponent(workpanel);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(SplitPane)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(SplitPane)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Userjpanel user = new Userjpanel();
SplitPane.setRightComponent(user);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
AdminJpanel add = new AdminJpanel();
SplitPane.setRightComponent(add);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Mainframe home = new Mainframe();
SplitPane.setRightComponent(home.workpanel);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Userjpanel user = new Userjpanel();
SplitPane.setRightComponent(user);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Mainframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Mainframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Mainframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Mainframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Mainframe().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JSplitPane SplitPane;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel workpanel;
// End of variables declaration
}
The code above is what I have used
Upvotes: 0
Views: 39
Reputation: 324098
Mainframe home = new Mainframe();
SplitPane.setRightComponent(home.workpanel);
In your "Back" button logic don't keep creating a new instance of you "Mainframe" class. You only ever need one instance of the frame.
You just need to access your "workpanel" instance in your class:
//Mainframe home = new Mainframe();
//SplitPane.setRightComponent(home.workpanel);
SplitPane.setRightComponent(workpanel);
Also, variable names should NOT start with a upper case character. Learn and follow Java naming conventions.
Upvotes: 1