基本上,这个程序是一个 9x9 矩阵,其中每个单元格都需要用特定的数字进行更新。我有一个矩阵表。将其初始化为 0。程序正在运行,但在完成所有操作后,表格只会更新一个。(找到解决方案)。
表格单元格没有得到更新,因为数据在后台发生变化。整个表在程序执行结束时得到更新。但是我想看到每个数字在我为每个单元格分配一个值时实时变化。非常感谢任何代码帮助。
我也尝试过没有容器。但这也没有更新表格。我所做的研究告诉我 setImmediate(true) 需要放在桌子上。但我已经有了。
还有论坛说
Item itt = container.getItem(cell.getRowIndex() + 1);
Property<Integer> rrr = itt.getItemProperty(cell.getColumnIndex() + 1);
rrr.setValue(num);
以上几行应该足以反映 UI 中的数字变化。但它没有发生。我也试过
table.refreshRowCache();
table.setCacheRate(0.1);
但是没有用。
我还在论坛中阅读了使用 valuechangelistner 的内容。但是,我找不到一个很好的例子来告诉我如何做到这一点,如果这是唯一的方法
private Container container = new IndexedContainer();
public class MyVaadinUI extends UI {
@Override
protected void init(VaadinRequest request) {
...
for (int i = 1; i <= 9; i++) {
container.addContainerProperty(i, Integer.class, 0);
}
for (int i = 1; i <= 9; i++) {
container.addItem(i);
}
...
table.setContainerDataSource(container);
table.setImmediate(true);
...
...
/ * this is filling the table correctly with all 0's */
populate.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
...
for (int i = 1; i <= 9; i++) {
Item item = container.getItem(i);
int rowIndex = i - 1;
for (int j = 1; j <= 9; j++) {
Property<Integer> nameProperty = item.getItemProperty(j);
int columnIndex = j - 1;
nameProperty.setValue(uploadReceiver.getMatrix()[rowIndex][columnIndex]);
}
}
}
});
solveButton.addClickListener(new ClickListener() {
private boolean solveSudoko() {
...
for (int num = 1; num <= 9; num++) {
// check if no conflicts then
if (!AreThereConflicts(num, cell.getRowIndex(), cell.getColumnIndex())) {
uploadReceiver.getMatrix()[cell.getRowIndex()][cell.getColumnIndex()] = num;
// Item row = container.getItem(cell.getRowIndex() + 1);
// Property<Integer> col =
// row.getItemProperty(cell.getColumnIndex() + 1);
// col.setValue(num);
Property prop = container.getItem(cell.getRowIndex() + 1).getItemProperty(cell.getColumnIndex() + 1);
prop.setValue(num);
table.setContainerDataSource(container);
/* Table is not getting updated here ???? */
...
uploadReceiver.getMatrix()[cell.getRowIndex()][cell.getColumnIndex()] = 0; // unassign
// Item itt = container.getItem(cell.getRowIndex() + 1);
// Property<Integer> rrr =
// itt.getItemProperty(cell.getColumnIndex() + 1);
// rrr.setValue(num);
Property prop2 = container.getItem(cell.getRowIndex() + 1).getItemProperty(cell.getColumnIndex() + 1);
prop2.setValue(num);
table.setContainerDataSource(container);
/* Table is not getting updated here ???? */
}
}
return false;
}