Does anyone know how to work with the Card Layout in the NetBeans GUI builder tool? I want to show panels as per the JRadioButton
selection, so I want to lay this out using the Card Layout.
card.next(yourPanel);
will loop through all the components in your mainpanel
then come to first one. To show a component with your own desire try following (think if there are 5 components and you are on the 2 and want to show first then you have to go through rest of all in the Vincent Ramdhanie's example, JRL's answer is good according to that gives a quick jump to one you want, but here is another way.
import javax.swing.JLabel;
import javax.swing.JPanel;
public class myJFrame extends javax.swing.JFrame {
private JPanel panel1, panel2;
/**
* Creates new form myJFrame
*/
public myJFrame() {
initComponents();
panel1=new JPanel();
panel2=new JPanel();
JLabel lb1=new JLabel("This is panel 1");
JLabel lb2=new JLabel("This is panel 2");
panel1.add(lb1);
panel2.add(lb2);
//make more if you want
// contentPanel.add(panel1);//show any of the panel first
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
buttonPanel1 = new javax.swing.JButton();
buttonPanel2 = new javax.swing.JButton();
contentPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
buttonPanel1.setText("Panel 1");
buttonPanel1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonPanel1ActionPerformed(evt);
}
});
buttonPanel2.setText("Panel 2");
buttonPanel2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonPanel2ActionPerformed(evt);
}
});
....
}
private void buttonPanel2ActionPerformed(java.awt.event.ActionEvent evt) {
contentPanel.removeAll();
contentPanel.add(panel2);
contentPanel.repaint();
contentPanel.revalidate();
}
private void buttonPanel1ActionPerformed(java.awt.event.ActionEvent evt) {
contentPanel.removeAll();
contentPanel.add(panel1);
contentPanel.repaint();
contentPanel.revalidate();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new myJFrame().setVisible(true);
}
});
}
private javax.swing.JButton buttonPanel1;
private javax.swing.JButton buttonPanel2;
private javax.swing.JPanel contentPanel;
private javax.swing.JPanel jPanel1;
}
This way is used when you have a tree and show a panel or component on a tree selection. It shows directly that component. On the tree add a value change listener
and get the selection item and show appropriate panel.