我创建了动态JCheckBox
,但我不知道如何检查它们的价值。
当我单击复选框时,我想获取价值并将其放入SQL查询。
like:query = select checkbox1, checkbox2 from table
这是我的动态复选框代码:
roll[K] = new JCheckBox();
roll[K].setText(metaData.getColumnLabel(columnIndex));
roll[K].setBounds(X,Y,150,30);
Y = Y+30;
Rectangle r = jPanel3.getBounds();
int h=r.height;
if (Y>=h-50){
Y=0;
X=X+200;
}
jPanel3.add(roll[K]);
答案 0 :(得分:0)
您可以这样设置复选框项目侦听器
roll[K].setActionCommand(Integer.toString(k));
roll[K].addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
//this is not necessary and depends upon you, how do u want
// use this
int id = Integer.parseInt(Integere.getSource().getActionCommand())
//can apply switch case on ID to take appropriate action based on ID
if (e.getStateChange() == 1)
System.out.println("Checked");
else
System.out.println("un-Checked");
}
});