验证并突出显示 JTable 单元格
我根据验证突出显示 JTable
单元格。在某些情况下,我必须采用其他列的值。例如,如果 column2
包含 USA,则 column3
应该只是数字。另一个例子,如果 col2
是“USA”并且 col4
是数字,那么 col5
应该只有三个字符。有人可以建议如何做到这一点吗?
在下面的片段中,col3
包含国家/地区名称; col4
和 col5
依赖于 col3
。当我处于 case 3
和 case 4
中时,我无法检查 case 2
的值。例如,我想要 if (col3.value == "USA")
。
[code]
tcol = editorTable.getColumnModel().getColumn(0);
tcol.setCellRenderer(new CustomTableCellRenderer());
tcol = editorTable.getColumnModel().getColumn(1);
tcol.setCellRenderer(new CustomTableCellRenderer());
tcol = editorTable.getColumnModel().getColumn(2);
tcol.setCellRenderer(new CustomTableCellRenderer());
public class CustomTableCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent (JTable table, Object
value,boolean isSelected, boolean hasFocus, int row, int col){
Component cell = super.getTableCellRendererComponent(table, value,
isSelected,hasFocus, row, col);
if (value instanceof String) {
String str = (String) value;
switch (col) {
case 0:
col1(str, cell);
break;
case 1:
col2(str, cell);
break;
case 2:
col3(str, cell);
break;
}
}
return cell;
}
private void col1(String str, Component cell) {
if(!str.matches("[0-9a-zA-z]")){
cell.setBackground(Color.RED);
} else {
cell.setBackground(Color.GREEN);
}
}
private void col2(String str, Component cell) {
if(!str.matches("[A-Z]{3}")){
cell.setBackground(Color.RED);
} else {
cell.setBackground(Color.GREEN);
}
}
[/code]
I am highlighting JTable
cells based on validation. In some conditions, I have to take the value of other columns. For example, if column2
has USA then column3
should be only numeric. As another example, if col2
is "USA" and col4
is numeric, then col5
should be only three chars. Can someone suggest how this can be done?
In the fragment below, col3
contains country names; col4
and col5
depend on col3
. When I am in case 3
and in case 4
, I cannot check the value of case 2
. For example, I want like, if (col3.value == "USA")
.
[code]
tcol = editorTable.getColumnModel().getColumn(0);
tcol.setCellRenderer(new CustomTableCellRenderer());
tcol = editorTable.getColumnModel().getColumn(1);
tcol.setCellRenderer(new CustomTableCellRenderer());
tcol = editorTable.getColumnModel().getColumn(2);
tcol.setCellRenderer(new CustomTableCellRenderer());
public class CustomTableCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent (JTable table, Object
value,boolean isSelected, boolean hasFocus, int row, int col){
Component cell = super.getTableCellRendererComponent(table, value,
isSelected,hasFocus, row, col);
if (value instanceof String) {
String str = (String) value;
switch (col) {
case 0:
col1(str, cell);
break;
case 1:
col2(str, cell);
break;
case 2:
col3(str, cell);
break;
}
}
return cell;
}
private void col1(String str, Component cell) {
if(!str.matches("[0-9a-zA-z]")){
cell.setBackground(Color.RED);
} else {
cell.setBackground(Color.GREEN);
}
}
private void col2(String str, Component cell) {
if(!str.matches("[A-Z]{3}")){
cell.setBackground(Color.RED);
} else {
cell.setBackground(Color.GREEN);
}
}
[/code]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@kleopatra 和@mKorbel 是正确的。您的片段不完整,但看起来好像您正在尝试解决渲染器中的编辑器和模型问题。
您可以验证自定义
TableCellEditor
中输入的值,如本示例所示。您可以处理TableModel
中的依赖列,如本示例。在评论中,您说:“如果我没记错的话,
prepareRenderer()
需要循环所有行, 正确的?”不,
JTable
“内部实现总是使用此方法来准备渲染器,以便子类可以安全地覆盖此默认行为。”当必须有选择地将更改应用于所有渲染器时,覆盖prepareRenderer()
非常有用。请参阅概念:编辑器和渲染器< /a> 了解更多详细信息。
@kleopatra and @mKorbel are correct. Your fragment is incomplete, but it appears as if you are trying to solve editor and model problems in the renderer.
You can validate entered values in a custom
TableCellEditor
, as shown in this example. You can handle dependent columns in theTableModel
, as shown in this example.In a comment you say, "If I am not wrong,
prepareRenderer()
needs looping of all the rows, right?"No,
JTable
"internal implementations always use this method to prepare renderers so that this default behavior can be safely overridden by a subclass." OverridingprepareRenderer()
is most useful when changes must be be selectively applied to all renderers.See Concepts: Editors and Renderers for more details.