0

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

Moore
  • 1
  • 3
  • 1
    Please create and post a valid [mcve] or [sscce](http://sscce.org). – Hovercraft Full Of Eels Jan 02 '17 at 17:33
  • thanks @HovercraftFullOfEels I added a sscce. please check again. – Moore Jan 02 '17 at 18:02
  • 1
    You are initializing your JRadioButton fields three separate times. You are creating nine JRadioButtons in total. – VGR Jan 02 '17 at 18:04
  • @VGR but if I didn't, JRadioButton doesn't show up on the card panels. I tried to initialize 3 JRadioButton only once then add them to 3 new JPanel buttonPanel. doesn't show up either. [can't have the same JComponent on two separate cards](http://www.dreamincode.net/forums/topic/227023-problem-with-card-layout/) – Moore Jan 02 '17 at 18:16
  • 1
    Correct. Put your JRadioButtons in a separate panel outside of the CardLayout. Of course, you could just forego the use of CardLayout and radio buttons, and instead use a JTabbedPane with [tab placement](http://docs.oracle.com/javase/8/docs/api/javax/swing/JTabbedPane.html#setTabPlacement-int-) set to JTabbedPane.BOTTOM, which would mean a lot less work for you. – VGR Jan 02 '17 at 18:23
  • To be more specific, a question about that [Can't a Swing component be added to multiple containers?](http://stackoverflow.com/questions/4620601/cant-a-swing-component-be-added-to-multiple-containers) – Moore Jan 02 '17 at 18:23
  • @VGR already knows this well. Either do as he suggests, or if you absolutely need to have new JRadioButtons on each JPanel, then have the corresponding JRadioButtons **share the same model**, ButtonModel to be precise, so that all first JRadioButtons have the same model, same for second,... etc. – Hovercraft Full Of Eels Jan 02 '17 at 18:28
  • oh great thanks! I didn't know about ButtonModel. Sorry I didn't see his comment before I replied. – Moore Jan 02 '17 at 18:41

0 Answers0