3

Can you tell me if I should be doing this differently? I need to make the last cell on my data rows a checkbox that is tied back to an object I will remove form the list when a delete button is clicked. When I manually create the TableModel in code and add a checkbox it renders in the cell as a "to-string" of the object rather than an actual checkbox.

So the question: is there another way to do this to make the CheckBox show as an actual checkbox instead?

String[] columnNames = {"Type","Qty","Raw TB","Usable TB","Del?"};
Object[][] cells = {{"TOTAL","","0","0",""},{"DD4500",1,0f,0f,new CheckBox()}};
Table cbSummaryTable = findSummaryTable(buildContainer);
TableModel tableModel = new DefaultTableModel(columnNames,cells);
cbSummaryTable.setModel(tableModel);

Table that rendered with the code above

Edit: I tried the suggestion from @ShaiAlmog, and while it looked promising (and reminded me of something I saw elsewhere) it seems to not have worked for some reason. the table now just says "true" and "false". I also tried a trick I'd seen about overriding TableModel to make a cell editable and that didn't help either.

New Code:

private void clearCBSummaryTable(Container buildContainer){
    String[] columnNames = {"Type","Qty","Raw TB","Usable TB","Del?"};
    Object[][] cells = {{"TOTAL","",0f,0f,Boolean.TRUE},{"DD4500",1,0f,0f,Boolean.FALSE}};
    Table cbSummaryTable = findSummaryTable(buildContainer);
    TableModel tableModel = new DefaultTableModel(columnNames,cells){
        @Override
        public boolean isCellEditable(int row, int column) {
            if (row >= 2 && column == 4){
                return true;
            }else {
                return super.isCellEditable(row, column);
            }
        }
    };
    cbSummaryTable.setModel(tableModel);
}

enter image description here

Anything else I can do to help the Table recognize and render the Boolean as a Checkbox?

Java-K
  • 497
  • 2
  • 11
  • `I need to make the last cell on my data rows a checkbox` Pass in a boolean value for that column, and have the TableModel override `getColumnClass` to return a Boolean.class for the column. See http://stackoverflow.com/questions/21242268/how-to-make-a-specific-jtable-boolean-column-editable – copeg Sep 12 '16 at 18:40
  • 1
    Possible duplicate of [How to add checkboxes to JTABLE swing](http://stackoverflow.com/questions/7391877/how-to-add-checkboxes-to-jtable-swing) – copeg Sep 12 '16 at 18:43
  • 1
    @copeg This is for Codename One not Swing. See https://www.codenameone.com/ the API is very similar to Swing but designed for mobile. – Shai Almog Sep 13 '16 at 03:02

1 Answers1

1

Try this:

 Object[][] cells = {
    {"TOTAL","","0","0",Boolean.FALSE},
    {"DD4500",1,0f,0f,Boolean.TRUE}
 };

The model contains data so setting the data to a boolean will implicitly format it in the table cell. You should not mix model and view (CheckBox is view)

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • Thanks Shai, I'll try that instead. Good point about model vs view. so then if that column is editable then I will be able to check and un-check then "read" the model later to determine what is selected? – Java-K Sep 14 '16 at 12:12
  • Yes you will be able to do that – Shai Almog Sep 15 '16 at 04:50
  • Shai, I tried what you suggested but I'm not sure what I'm doing wrong, it renders as a string "true" or "false". I updated my question above with details on what I tried. Can you please have another look? – Java-K Sep 16 '16 at 19:41
  • I think it's because the GUI builder did something with the table. Try not adding it from the GUI builder and adding it from code instead. Construct the table with the proper model and add it into place. – Shai Almog Sep 17 '16 at 05:09