简体   繁体   中英

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 :

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

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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