dimanche 20 mai 2018

Create 'x' number of rows of checkboxes in Java

import java.awt.Button;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Status extends JFrame implements ActionListener
{
    private TextField tf1 = new TextField();
    private Button btn = new Button("Deploy");
    JPanel grid;
    public Status()
    {
        new JPanel(new GridLayout(0, 2));
        tf1.setSize(new Dimension(40,40));
        btn.setSize(new Dimension(30,30));

        grid.add(tf1);
        grid.add(btn);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getSource() == btn)
        {
            int x = Integer.parseInt(tf1.getText());
            //Checkbox [][] a = {new Checkbox("1"};
            for (int i = 0; i < x; i++)
            {
                JCheckBox jc = new JCheckBox(""+i); 
                grid.add(jc);
            }

        }

    }

}

This is my code. I want to create 'x' number of rows of Checkboxes, where 'x' is the numeric value input by the user through a TextField. Each row of the checkbox (no matter what the value), should contain 8 Checkboxes each. Please help!




Aucun commentaire:

Enregistrer un commentaire