3
votes

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?

1
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 stackoverflow.com/questions/21242268/… - copeg
@copeg This is for Codename One not Swing. See codenameone.com the API is very similar to Swing but designed for mobile. - Shai Almog

1 Answers

1
votes

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)