Search code examples
javaswingooplistenerjcheckbox

displaying selected jcheckbox in a jlabel


i have 10 jcheckbox and only 5 should be selected. i already did all the coding for this one, but i don't know how to display the selected 5 into a jlabel. i tried doing it by this code:

           JCheckBox check;
           JPanel panel=new JPanel();
           for(int i=0; i<10; i++){
                check=new JCheckBox();
                check.addActionListener(listener);
                check.setName("Select"+i);

                panel.add(check); 
            }

this is the listener

ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            check = (JCheckBox) e.getSource();
            name=check.getName();
        }
    };

and this is the panel where it should be displayed into jlabel

        panel2=new JPanel(new GridLayout(5,1));
        for(int i=0; i<5; i++){
            txtVote=new JLabel(name);
            panel2.add(txtVote);
        }

but using this code, it doesn't display anything on the jlabel. if i change the listener into this:

ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                check = (JCheckBox) e.getSource();
                txtVote.setText(check.getName());
            }
        };

it will only display into the last label. other jlabels would be blank. please help thank you so much

EDIT

here is the code that is runnable

public class JCheckBoxtoLabel{
    JCheckBox check;
    String name;
    JLabel txtVote;

     public JCheckBoxtoLabel() {
            JFrame frame = new JFrame();
            JPanel panel = createPanel();
            JPanel panel2 = panel2();

            frame.setLayout(new GridLayout(1,2));
            frame.add(panel); frame.add(panel2);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(500, 300);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

        private JPanel createPanel() {
               JPanel panel=new JPanel(new GridLayout(10,1));
               for(int i=0; i<10; i++){
                    check=new JCheckBox();
                    check.addActionListener(listener);
                    check.setName("Select"+i);

                    panel.add(check); 
                }

            return panel;
        }

        private JPanel panel2(){
            JPanel panel2=new JPanel(new GridLayout(5,1));
            for(int i=0; i<5; i++){
                txtVote=new JLabel();
                txtVote.setBorder(BorderFactory.createLineBorder(Color.RED));
                panel2.add(txtVote);
            }
         return panel2;
     }

        ActionListener listener = new ActionListener() {
            @Override   
            public void actionPerformed(ActionEvent e) {
                check = (JCheckBox) e.getSource();
                txtVote.setText(check.getName());
            }
        };

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new JCheckBoxtoLabel();
                }
            });
        }
}

Solution

  • The problem is that txtVote is only a single Jlabel and you are trying to use it for all 5. Since the fifth jlabel was the last to be created it is the one being used. My suggestion is that you create an arraylist field and inside panel12 add each label to the arraylist. Then inside the listener it would iterate through each jlabel in the arraylist check if has text set to it, if so check the next one until it finds one with no text, then sets the text to that. The problem with this at the moment is that in your code you are never defining what happens when they uncheck the box.

    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    
    import javax.swing.BorderFactory;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class JCheckBoxtoLabel{
    JCheckBox check;
    String name;
    JLabel txtVote;
    ArrayList<JLabel> boxLabels;
    
    public JCheckBoxtoLabel() {
        boxLabels = new ArrayList<JLabel>();
    
        JFrame frame = new JFrame();
        JPanel panel = createPanel();
        JPanel panel2 = panel2();
    
        frame.setLayout(new GridLayout(1,2));
        frame.add(panel); frame.add(panel2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    private JPanel createPanel() {
        JPanel panel=new JPanel(new GridLayout(10,1));
        for(int i=0; i<10; i++){
            check=new JCheckBox();
            check.addActionListener(listener);
            check.setName("Select"+i);
    
            panel.add(check); 
        }
    
        return panel;
    }
    
    private JPanel panel2(){
        JPanel panel2=new JPanel(new GridLayout(5,1));
        for(int i=0; i<5; i++){
            txtVote=new JLabel();
            txtVote.setBorder(BorderFactory.createLineBorder(Color.RED));
            panel2.add(txtVote);
            boxLabels.add(txtVote);
        }
        return panel2;
    }
    
    ActionListener listener = new ActionListener() {
        @Override   
        public void actionPerformed(ActionEvent e) {
            check = (JCheckBox) e.getSource();
            if(!check.isSelected()){
                for(JLabel label: boxLabels){
                    if(label.getText().equals(check.getName())) label.setText("");
                }
            }else{
                for(JLabel label: boxLabels){
                    if(label.getText().isEmpty()){
                        label.setText(check.getName());
                        return;
                    }
                }
            }
        }
    };
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JCheckBoxtoLabel();
            }
        });
    }
    }