Java 删除或隐藏单元格中的值
有没有办法隐藏或删除某个单元格或整列的值? 如果你看这张照片: 您将看到,在向表中添加模拟数据时,我非常有创意 撇开所有笑话不谈,我想删除优先级列中的所有值,所以只有彩色单元格。可能吗?可能会遍历整个列并将值设置为null?只是一个没有实现计划的想法 编辑Java 删除或隐藏单元格中的值,java,swing,user-interface,jtable,Java,Swing,User Interface,Jtable,有没有办法隐藏或删除某个单元格或整列的值? 如果你看这张照片: 您将看到,在向表中添加模拟数据时,我非常有创意 撇开所有笑话不谈,我想删除优先级列中的所有值,所以只有彩色单元格。可能吗?可能会遍历整个列并将值设置为null?只是一个没有实现计划的想法 编辑 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(value.equals("1")) { // red
cell.setBackground(Color.RED);
cell.setForeground(Color.WHITE);
super.setValue(null);
} else if (value.equals("2")) { // yellow
cell.setBackground(Color.ORANGE);
cell.setForeground(Color.WHITE);
super.setValue(null);
} else if (value.equals("3")) { // green
cell.setBackground(Color.GREEN);
cell.setForeground(Color.WHITE);
super.setValue(null);
} else { // white
cell.setBackground(Color.WHITE);
cell.setForeground(Color.GRAY);
super.setValue(null);
}
return cell;
}
我的手机渲染器:
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(value.equals("1")) { // red
cell.setBackground(Color.RED);
cell.setForeground(Color.WHITE);
} else if (value.equals("2")) { // yellow
cell.setBackground(Color.ORANGE);
cell.setForeground(Color.WHITE);
} else if (value.equals("3")) { // green
cell.setBackground(Color.GREEN);
cell.setForeground(Color.WHITE);
} else { // white
cell.setBackground(Color.WHITE);
cell.setForeground(Color.GRAY);
}
return cell;
}
将渲染器指定给表:
TableColumn tblColumn = this.tblTodo.getColumnModel().getColumn(1);
tblColumn.setCellRenderer(new PriorityColumnCellRenderer());
您只需将渲染器的
gettableCellRenderComponent(…)
方法中的super方法调用中的value
的值更改为空字符串:“”
:
e、 g
找到了答案
super的类中有一个方法:
super.setValue("myValue");
在我的情况下,这将如下所示:
super.setValue(null);
但需要在检查值后调用此函数。最终代码:
编辑
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(value.equals("1")) { // red
cell.setBackground(Color.RED);
cell.setForeground(Color.WHITE);
super.setValue(null);
} else if (value.equals("2")) { // yellow
cell.setBackground(Color.ORANGE);
cell.setForeground(Color.WHITE);
super.setValue(null);
} else if (value.equals("3")) { // green
cell.setBackground(Color.GREEN);
cell.setForeground(Color.WHITE);
super.setValue(null);
} else { // white
cell.setBackground(Color.WHITE);
cell.setForeground(Color.GRAY);
super.setValue(null);
}
return cell;
}
编辑
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(value.equals("1")) { // red
cell.setBackground(Color.RED);
cell.setForeground(Color.WHITE);
super.setValue(null);
} else if (value.equals("2")) { // yellow
cell.setBackground(Color.ORANGE);
cell.setForeground(Color.WHITE);
super.setValue(null);
} else if (value.equals("3")) { // green
cell.setBackground(Color.GREEN);
cell.setForeground(Color.WHITE);
super.setValue(null);
} else { // white
cell.setBackground(Color.WHITE);
cell.setForeground(Color.GRAY);
super.setValue(null);
}
return cell;
}
为了简单起见,我使用了的答案为什么不将渲染器设置为精确显示您希望它显示的内容?这可能是一种方法,但如何实现呢?我用代码编辑了我的帖子,当你调用super的方法时,你可以在你的单元格中设置
value=“
。Component cell=super.gettableCellRenderComponent(表“”,isSelected,hasFocus,行,列)代码>根据我的回答,我只会在渲染器中执行此操作。是的,它在渲染器中,代码现在已完全显示。谢谢,谢谢。在我的例子中,这需要在检查值之后单独完成,正如我在我的answer@SebastianDudas:没有。请参阅上面的渲染器代码。尽量简单。哦,就是这样。。。没有意识到这是新的目标。那是固定的,谢谢