Swing中创建动态jCheckBox

Swing中创建动态jCheckBox

本文介绍了如何在java Swing中创建动态jCheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何动态创建jcheckbox;

i在组合框中获取表名,我想添加与所选表的列数一样多的复选框。

请帮助我;

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;

推荐答案

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

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





我希望这会对你有所帮助。



Beste问候,

菲利普



I hope this helps you.

Beste regards,
Filipe


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>





祝你好运。



我使用了Filipe显示的想法。



Best regards.

I have used the idea that Filipe displayed.


这篇关于如何在java Swing中创建动态jCheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:35