Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Checkbox to CodenameOne TableModel in Table component

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?

like image 232
Java-K Avatar asked Feb 20 '26 20:02

Java-K


1 Answers

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)

like image 112
Shai Almog Avatar answered Feb 23 '26 09:02

Shai Almog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!