how to get value from ticked jcheckbox in a row from jtable


拟墨画扇 提交于 2019-12-20 06:34:07

问题


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));

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)?

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

回答1:


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:

  • Insert values of type Boolean.class in your TableModel.

  • Return Boolean.class from getColumnClass() for the relevant column.

  • Return the desired value from isCellEditable().



来源:https://stackoverflow.com/questions/17255334/how-to-get-value-from-ticked-jcheckbox-in-a-row-from-jtable

标签

工具导航Map