-2

the question is :How can i add a button on each row in a table in CODENAME ONE? and as u can see in the line 19 i commented where the code should be to add a button inside col4

Form hi = new Form("Table", new BorderLayout());
TableModel model = new DefaultTableModel(new String[] {"Col 1", "Col 2", "Col 3","col4"}, new Object[][] {
{"Row 1", "Row A", "Row X"},
{"Row 2", "Row B can now stretch", null},
{"Row 3", "Row C", "Row Z"},
{"Row 4", "Row D", "Row K"},
}) {
    public boolean isCellEditable(int row, int col) {
        return col != 0;
    }
};
Table table = new Table(model) {
@Override
protected TableLayout.Constraint createCellConstraint(Object value, int row, int column) {
    TableLayout.Constraint con =  super.createCellConstraint(value, row, column);
    if(column == 3) {
        //how can i add a button here on each row ?
    }
    con.setWidthPercentage(33);
    return con;
}
};
hi.add(BorderLayout.CENTER, table);
mohamed lahsoumi
  • 735
  • 1
  • 5
  • 12

1 Answers1

1

It's simple return a new Button but not from that method. You need to override createCell not the constraint method:

protected Component createCell(Object value, int row, int column, boolean editable) { 
    if(row > -1 && column == 3) {  
        Button value = new Button((String)value);
        return value;
    }
    return super.createCell(value, row, column, editable);;
}

There's a more complete sample returning a picker here: https://www.codenameone.com/blog/understanding-the-table-component.html

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • there two returns in this method this is an unreachable statement : return super.createCell(value, row, column, editable);; And also variable value is already defiend in the method – mohamed lahsoumi May 13 '20 at 10:15