如何在 CODENAME ONE 的 table 中的每一行添加一个按钮?
How can i add a button on each row in a table in CODENAME ONE?
问题是:如何在 CODENAME ONE 的 table 中的每一行添加一个按钮?正如你在第 19 行中看到的那样,我评论了代码应该在 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);
这很简单 return 一个新的 Button
但不是来自那个方法。你需要覆盖
createCell
不是约束方法:
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);;
}
这里有一个更完整的示例return选择器:https://www.codenameone.com/blog/understanding-the-table-component.html
问题是:如何在 CODENAME ONE 的 table 中的每一行添加一个按钮?正如你在第 19 行中看到的那样,我评论了代码应该在 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);
这很简单 return 一个新的 Button
但不是来自那个方法。你需要覆盖
createCell
不是约束方法:
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);;
}
这里有一个更完整的示例return选择器:https://www.codenameone.com/blog/understanding-the-table-component.html