Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how can i create jcheckboxes dynamically;
i am getting table name in a comboBox and i want to add as many checkboxes as the number of columns the selected table have.
pls help me out;
Posted
Comments
TorstenH. 10-May-13 2:00am    
...Do you want to have checkboxes in the combo or what?

Hi Kapilk,

Just use an array. You read the number of of columns from the combobox and then create an array of JCheckBox:

Java
int numberCheckBox = ...
JCheckBox[] checkBoxList = new JCheckBox[numberCheckBox];

for(int i = 0; i < numberCheckBox; i++) {
    checkBoxList[i] = new JCheckBox("CheckBox" + i);
    yourPanel.add(checkBoxList[i]);
}


I hope this helps you.

Beste regards,
Filipe
 
Share this answer
 
Use this code, I have written it myself and it works:
Java
import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;


public class Tabel extends JFrame{

	/**
	 * @param args
	 */
	int numberOfCheckBoxes=0;  //this is used to get the number of items in the combobox
	JCheckBox[] checkBoxList;   //the list that contains the checkboxes
	
	String[] columns=new String[]{"Column 1","Column 2","Column 3"};  //something for the table to display
	Object data[][]={
			{true,new Integer(10),new String("String 1")},
			{false,new Integer(20),new String("String 2")},
			{true,new Integer(30),new String("String 3")}
	};

	JTable table;
	JPanel firstPanel=new JPanel();  //this is just for gui to look better
	JPanel secondPanel=new JPanel();
	
	@SuppressWarnings("unchecked")
	public void initialize(){
		
		table=new JTable(data,columns);
		this.setTitle("Tabel");
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(500,200);
		
		
		firstPanel.setLayout(new BoxLayout(firstPanel,BoxLayout.Y_AXIS));
		firstPanel.setBorder(new TitledBorder("Table"));
		
		
		secondPanel.setLayout(new BoxLayout(secondPanel,BoxLayout.Y_AXIS));
		secondPanel.setBorder(new TitledBorder("GUI Items"));
		
		firstPanel.add(table.getTableHeader(),BorderLayout.PAGE_START); //if you don't use this line, the column names won't be displayed in the window

		firstPanel.add(table);
						
		ArrayList<string> columnNames=new ArrayList<string>(); //list for containg the column names
		
		for(int i=0;i<table.getcolumncount();i++){
		columnNames.add(table.getColumnName(i));
		}
		
		JComboBox<?> comboBox=new JComboBox(columnNames.toArray());		
		numberOfCheckBoxes=comboBox.getItemCount();  //get the number of items in combobox
		
		secondPanel.add(comboBox);
		
		checkBoxes();  //method for creating the checkboxes
		
		this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
		this.add(firstPanel);
		this.add(secondPanel);
		
		
	}
	
	void checkBoxes(){
		checkBoxList=new JCheckBox[numberOfCheckBoxes];
		
		for(int i=0;i<checkboxlist.length;++i){>
			checkBoxList[i]=new JCheckBox("CheckBox #"+i); //create the check boxes and add them to the jframe
			secondPanel.add(checkBoxList[i]);
						
		}
		
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
			Tabel program=new Tabel();
			program.initialize();
	}

}


</string></string>


Best regards.

I have used the idea that Filipe displayed.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900