简体   繁体   English

如何从jtable的行中的选中的jcheckbox中获取值

[英]how to get value from ticked jcheckbox in a row from jtable

I have a table with 3 column and dynamic row based from database value and a jcheckbox in last column based on this code : 我有一个表,该表具有基于数据库值的三列和动态行,并且基于此代码在最后一列中有一个jcheckbox:

  TableColumn tcolumn = tabel.getColumnModel().getColumn(2);
     tcolumn.setCellRenderer(tabel.getDefaultRenderer(Boolean.class));
       tcolumn.setCellEditor(tabel.getDefaultEditor(Boolean.class));

example of my table: 我的桌子的例子:

============================================
val 1 || val 2 || val 3 (checkbox) ||
============================================

from FB || from DB || checkbox           ||

from DB || from DB || checkbox           ||

===========================================     

My question is simple, how can I get all of the value 1 from the ticked checkbox in column 2 (value 3)? 我的问题很简单,如何从第2列(值3)的复选框中得到所有的值1?

I tried many simple code but still got an error. 我尝试了许多简单的代码,但仍然出现错误。

this is my code: 这是我的代码:

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)+" || ");
    }
  } 

It's not clear what error you get or where you get it; 目前尚不清楚您会得到什么错误或在哪里得到的错误。 I suspect an error casting to Boolean . 我怀疑将错误转换为Boolean As general guidance, the default renderer and editor for Boolean.class is a JCheckbox ; 作为一般指导, Boolean.class默认渲染器和编辑器JCheckbox you shouldn't have to set it explicitly. 您不必显式设置它。 As shown here , ensure that you observe the following principles for your cast to succeed: 如图所示这里 ,请确保您遵守以下原则,你的施法成功:

  • Insert values of type Boolean.class in your TableModel . TableModel插入Boolean.class类型的值。

  • Return Boolean.class from getColumnClass() for the relevant column. getColumnClass()返回相关列的Boolean.class

  • Return the desired value from isCellEditable() . isCellEditable()返回所需的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM