0

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)+" || ");
    }
  } 
MaveRickZ
  • 37
  • 9
  • Please edit your question and show what have you tried. – Luiggi Mendoza Jun 22 '13 at 21:12
  • I can't really explain your problem, question is - can you? – Elist Jun 22 '13 at 21:20
  • Looks like you already had those values in your `String[] fieldData`, now what do you need? – Luiggi Mendoza Jun 23 '13 at 13:01
  • if i marked 1st row, it get the value but the output show error. i want to, example : if i marked first row, third row, and 5th row, it get value from their colomn 0. ==> 1011501100,1011501102,1011501104. but it still get error. is my logic in that code wrong ? – MaveRickZ Jun 23 '13 at 13:09
  • i got error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException and the 3rd column absolutelly a boolean value, but it nothing to do/not linking to database, it just a condition, if i ticked, then my logic are run (System.out.print(tblModel.getValueAt(row, 0)+" || ");) – MaveRickZ Jun 23 '13 at 20:24
  • You still haven't posted a [SSCCE](http://sscce.org/). That is how you improve a question. That is you show a simple program with a table a button and the code you execute when you click the button. Then maybe we can help explain why your looping logic isn't working. – camickr Jun 23 '13 at 22:26
  • @user2512451: As you have complied with Hovercraft's [request](http://stackoverflow.com/q/17264828/230513), I'll hazard a [guess](http://stackoverflow.com/a/17265891/230513). – trashgod Jun 23 '13 at 22:31

1 Answers1

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

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • @Volatil3: Some examples are seen [here](http://stackoverflow.com/search?tab=votes&q=user%3a230513%20%5bjtable%5d%20DefaultTableModel%20Boolean.class). – trashgod Jul 20 '14 at 01:28