So the problem is: I'm trying to make a wizard-like CardLayout. In each card panel, I put back & next JButton and 3 JRadioButton to switch between 3 pages.
Now, when I select the radio buttons the 1st time, it works normally. However, the 2nd time I select the radio button, they don't get selected as expected. For example, I want to select page 2, the card panel 2 does show up, but the radio button 2 state does not show that it's being selected, instead either radio button 1 or 3 gets selected. Button 2 only gets selected when I click it again. Same thing happens when I try to select the others.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutWizardDemo extends JFrame{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CardLayoutWizardDemo frame= new CardLayoutWizardDemo();
frame.init();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
private static final long serialVersionUID = 1L;
private JPanel cardPanel, panel1, panel2, panel3, btnPanel1, btnPanel2, btnPanel3;
private JLabel label1, label2, label3;
private JRadioButton step1, step2, step3;
private ButtonGroup bg;
private CardLayout cl = new CardLayout();
private void init(){
setTitle("CardLayoutWizardDemo");
cardPanel = new JPanel();
cardPanel.setLayout(cl);
panel1 = new JPanel(new BorderLayout());
panel2 = new JPanel(new BorderLayout());
panel3 = new JPanel(new BorderLayout());
label1 = new JLabel("label 1");
label2 = new JLabel("label 2");
label3 = new JLabel("label 3");
panel1.add(label1, BorderLayout.NORTH);
panel2.add(label2, BorderLayout.NORTH);
panel3.add(label3, BorderLayout.NORTH);
btnPanel1 = new JPanel();
btnPanel2 = new JPanel();
btnPanel3 = new JPanel();
btnPanel1.setName("panel1");
btnPanel2.setName("panel2");
btnPanel3.setName("panel3");
btnPanel1 = initTutBtn(btnPanel1);
btnPanel2 = initTutBtn(btnPanel2);
btnPanel3 = initTutBtn(btnPanel3);
panel1.add(btnPanel1, BorderLayout.SOUTH);
panel2.add(btnPanel2, BorderLayout.SOUTH);
panel3.add(btnPanel3, BorderLayout.SOUTH);
cardPanel.add(panel1, "1");
cardPanel.add(panel2,"2");
cardPanel.add(panel3,"3");
getContentPane().add(cardPanel, BorderLayout.CENTER);
setPreferredSize(new Dimension(350,500));
setMinimumSize(new Dimension(240,320));
pack();
setLocationByPlatform(true);
}
/**create new set of 3 step buttons
*/
private JPanel initTutBtn(JPanel btnPanel){
btnPanel.setLayout(new BoxLayout(btnPanel,BoxLayout.X_AXIS));
step1 = new JRadioButton();
step2 = new JRadioButton();
step3 = new JRadioButton();
step1.setActionCommand("step1");
step2.setActionCommand("step2");
step3.setActionCommand("step3");
bg = new ButtonGroup();
bg.add(step1);
bg.add(step2);
bg.add(step3);
if (btnPanel.getName().equals("panel1")){
step1.setSelected(true);
}else if (btnPanel.getName().equals("panel2")){
step2.setSelected(true);
}else if (btnPanel.getName().equals("panel3")){
step3.setSelected(true);
}
step1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
goToStep(e);
}
});
step2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
goToStep(e);
}
});
step3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
goToStep(e);
}
});
btnPanel.add(step1);
btnPanel.add(step2);
btnPanel.add(step3);
return btnPanel;
}
private void goToStep(ActionEvent evt){
if(evt.getActionCommand().equals("step1")){
cl.show(cardPanel, "1");
}else if(evt.getActionCommand().equals("step2")){
cl.show(cardPanel, "2");
}else if(evt.getActionCommand().equals("step3")){
cl.show(cardPanel, "3");
}
}
}
I think maybe the problems lie where I create new radio buttons within initButton()
and goToStep(ActionEvent evt)
but I can't figure out what I did wrong