displaying selected jcheckbox in a jlabel
<p>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:</p>

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

                panel.add(check); 
            }
</code></pre>

<p>this is the <code>listener</code></p>

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

<p>and this is the panel where it should be displayed into jlabel</p>

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

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

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

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

<p><strong>EDIT</strong></p>

<p>here is the code that is runnable</p>

<pre><code>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&lt;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&lt;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();
                }
            });
        }
}
</code></pre>