如何动态更改jtable单元格背景 [英] How to change jtable cell Background Dynamically
本文介绍了如何动态更改jtable单元格背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个5行5列的 JTable table1
,我想当我调用
I am having a JTable table1
with 5 rows and 5 columns, and I would like to
change the background color of 3rd column/cell of 2nd row, when I call a function like
changeBgColor(row,col);
changeBgColor(row,col);
这可能吗?
推荐答案
覆盖 示例:
public Component prepareRenderer (TableCellRenderer renderer, int rowIndex, int columnIndex){
Component componenet = super.prepareRenderer(renderer, rowIndex, columnIndex);
if(rowIndex % 2 == 0) {
componenet.setBackground(Color.RED);
} else {
componenet.setBackground(Color.GREEN);
}
return componenet;
}
在这里,我将偶数位置的所有行都着色为 RED
,将奇数位置的所有行都着色为 GREEN
.
Here I am coloring all the rows at even positions as RED
and all the rows at odd positions as GREEN
.
就您的问题而言.使用相同的方法,只是使用约束条件,
As far as your problem is considered. Use the same approach just use a constraint stating,
if(rowIndex == 2 && columnIndex == 3) {
componenet.setBackground(Color.RED);
}
除上述单元格外,所有单元格都将使用默认颜色.
Other than the above stated cell all the cells will get the default color.
这篇关于如何动态更改jtable单元格背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文