我如何 select 并突出显示代号一中的 Table 中的一行
How can I select and hightlight a row in a Table in Codename One
我是代号一的新手,也是在 Stack Overflow 上提问的新手。我觉得两个都很棒!
我对代号一中的 Table class 有疑问。我搜索了网络、Stack Overflow、API 等所有内容,但找不到好的答案。
我希望能够 select 一行并将其突出显示,以便我随后可以对其中的数据执行操作。我已经通过覆盖 createCell
方法并制作所有单元格按钮来使其工作,但这不是 acceptable 方法。问题是因为每次用户 'clicks' 单元格时都必须重建 table。
当 table 包含的行多于可以显示的行数时,table 被重建,table 跳回到 table 的开头。如果 s/he 想要查看,用户必须向下滚动到 selected 行。让 table 像那样跳来跳去是 unacceptable。
此外,我可能想使用 table 中的其他控件。我知道有一个 table.getSelectedRow()
,所以我可能遗漏了什么。另外,我想知道如何以编程方式 select 一行。
非常感谢任何帮助!
这基于 Table
的 javadoc 中的示例。我做的一个小技巧是 setModel(getModel())
手动触发 table 的刷新:
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();
我将使用的解决方案如下:
- 对于单个选择,第一列中的单选按钮。
- 对于多选,第一列中的复选框。
我还是很好奇 com.codename1.ui.table.Table class 中的 getSelectedRow() 是干嘛的。
我希望这对其他人的 GUI 设计有所帮助。
我是代号一的新手,也是在 Stack Overflow 上提问的新手。我觉得两个都很棒!
我对代号一中的 Table class 有疑问。我搜索了网络、Stack Overflow、API 等所有内容,但找不到好的答案。
我希望能够 select 一行并将其突出显示,以便我随后可以对其中的数据执行操作。我已经通过覆盖 createCell
方法并制作所有单元格按钮来使其工作,但这不是 acceptable 方法。问题是因为每次用户 'clicks' 单元格时都必须重建 table。
当 table 包含的行多于可以显示的行数时,table 被重建,table 跳回到 table 的开头。如果 s/he 想要查看,用户必须向下滚动到 selected 行。让 table 像那样跳来跳去是 unacceptable。
此外,我可能想使用 table 中的其他控件。我知道有一个 table.getSelectedRow()
,所以我可能遗漏了什么。另外,我想知道如何以编程方式 select 一行。
非常感谢任何帮助!
这基于 Table
的 javadoc 中的示例。我做的一个小技巧是 setModel(getModel())
手动触发 table 的刷新:
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();
我将使用的解决方案如下:
- 对于单个选择,第一列中的单选按钮。
- 对于多选,第一列中的复选框。
我还是很好奇 com.codename1.ui.table.Table class 中的 getSelectedRow() 是干嘛的。
我希望这对其他人的 GUI 设计有所帮助。