如何从jtable中连续勾选jcheckbox获取值 [英] how to get value from ticked jcheckbox in a row from jtable
问题描述
我有一个基于此代码的基于数据库值的3列和动态行的表以及最后一列中的jcheckbox:
I have a table with 3 column and dynamic row based from database value and a jcheckbox in last column based on this code :
TableColumn tcolumn = tabel.getColumnModel().getColumn(2);
tcolumn.setCellRenderer(tabel.getDefaultRenderer(Boolean.class));
tcolumn.setCellEditor(tabel.getDefaultEditor(Boolean.class));
我的表格示例:
============================================
val 1 || val 2 || val 3 (checkbox) ||
============================================
from FB || from DB || checkbox ||
from DB || from DB || checkbox ||
===========================================
我的问题很简单,如何从列中的勾选复选框中获取所有值1 2(价值3)?
My question is simple, how can I get all of the value 1 from the ticked checkbox in column 2 (value 3)?
我尝试了很多简单的代码,但仍然出错。
I tried many simple code but still got an error.
这是我的代码:
for (int row =0; row <= tabel.getSelectedRowCount(); row++) {
Boolean b = ((Boolean) tblModel.getValueAt(row, 2));
if (b.booleanValue()) {
System.out.print(tblModel.getValueAt(row, 0)+" || ");
}
}
推荐答案
这是不清楚你得到的错误或你得到的错误;我怀疑错误转换为 Boolean
。作为一般指导,默认渲染器和编辑器 Boolean.class
是 JCheckbox
;你不应该明确地设置它。如此处所示,请确保您遵循以下原则让您的演员阵容成功:
It's not clear what error you get or where you get it; I suspect an error casting to Boolean
. As general guidance, the default renderer and editor for Boolean.class
is a JCheckbox
; you shouldn't have to set it explicitly. As shown here, ensure that you observe the following principles for your cast to succeed:
-
在
TableModel $ c $中插入
Boolean.class
类型的值c>。
Insert values of type
Boolean.class
in yourTableModel
.
从 getColumnClass()返回
Boolean.class
相关列的code>。
Return Boolean.class
from getColumnClass()
for the relevant column.
从 isCellEditable()
返回所需的值。
这篇关于如何从jtable中连续勾选jcheckbox获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!