TechHelp
TechHelp

Reputation: 217

How to create multiple check boxes in for loop in Java?

I am writing a Java program in which I have to create multiple check boxes in for loop like this:

123 409 123 []
234 587 344 []
342 534 343 []

I have to use only those values in a row which is checked. Can you help me how to create multiple check boxes than how use selected values from that row?

Suppose first row is selected than i want all three values.

I have to create at least 30 checkboxs in one loop and there are four loops. I have no idea how to implement it.

Upvotes: 1

Views: 4414

Answers (1)

giampaolo
giampaolo

Reputation: 6934

I implemented a little raw example that should help you to get your result. I suggest you to try this and if it satisfies your needs, I will be glad to give you more details and edit my answer. Keep in mind that if you need to have only one row selected, you should prefer JRadioButton.

package stackoverflow.answers;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.util.*;

import javax.swing.*;

public class SwingCheckBoxDemo extends JPanel implements ItemListener {

    private HashMap<JCheckBox, ArrayList<Integer>> map = new HashMap<>();
    private JLabel _label;

    private static final int MAX_CHECKS = 30;

    public SwingCheckBoxDemo() {
        super(new BorderLayout());

        JCheckBox checkBox;
        Random r = new Random();

        JPanel checkPanel = new JPanel(new GridLayout(0, 1));
        _label = new JLabel("You selected nothing");
        checkPanel.add(_label);

        for (int i = 0; i < MAX_CHECKS; i++) {
            StringBuilder sb = new StringBuilder();
            ArrayList<Integer> a = new ArrayList<>();
            for (int j = 0; j < 3; j++) {
                Integer temp = (r.nextInt()) % 100;
                a.add(temp);
                sb.append(temp).append(" ");
            }

            checkBox = new JCheckBox(sb.toString().trim());
            checkBox.setName("CheckBox" + i);
            checkBox.addItemListener(this);
            map.put(checkBox, a);
            checkPanel.add(checkBox);
        }

        add(checkPanel);

    }

    public void itemStateChanged(ItemEvent e) {

        JCheckBox source = (JCheckBox) e.getItemSelectable();

        if (e.getStateChange() == ItemEvent.SELECTED) {

            ArrayList<Integer> list = map.get(source);

            _label.setText("You've just selected " + list);

        }

    }

    private static void createAndShowGUI() {

        JFrame _frame = new JFrame("Check box loop");
        _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        _frame.setSize(800, 600);

        JComponent newContentPane = new SwingCheckBoxDemo();
        newContentPane.setOpaque(true);
        _frame.setContentPane(newContentPane);

        _frame.setVisible(true);
    }

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

Upvotes: 1

Related Questions