這是基於來自javadocs的樣本Table
。在一個較小的黑客我所做的就是setModel(getModel())
觸發表的刷新手動:
Form hi = new Form("Table", new BorderLayout());
TableModel model = new DefaultTableModel(new String[] {"Col 1", "Col 2", "Col 3"}, 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) {
private int selectedRow = -1;
@Override
protected Component createCell(Object value, int row, int column, boolean editable) {
Component cell;
if(row < 0) {
cell = super.createCell(value, row, column, editable);
} else {
cell = new Button(value.toString());
cell.setUIID("TableCell");
((Button)cell).addActionListener(e -> {
selectedRow = row;
setModel(getModel());
});
}
if(selectedRow > -1 && selectedRow == row) {
cell.getAllStyles().setBgColor(0xff0000);
cell.getAllStyles().setBgTransparency(100);
}
return cell;
}
@Override
protected TableLayout.Constraint createCellConstraint(Object value, int row, int column) {
TableLayout.Constraint con = super.createCellConstraint(value, row, column);
if(row == 1 && column == 1) {
con.setHorizontalSpan(2);
}
con.setWidthPercentage(33);
return con;
}
};
hi.add(BorderLayout.CENTER, table);
hi.show();
謝謝你的回答。在對該主題進行研究並提出問題之前,我提出了相同的解決方案。但是,它不符合我的所有要求。我確實想出了一個替代方案,我將作爲答案發布。再次感謝你的幫助。 – Chuck