swing如果使用java满足特定条件,如何设置特定行的颜色?
我有一个jtable。(表摘要)。其中一列是过期。我想强调的是,客户的到期日在当前日期已经过期
我已经知道了逻辑,但我不能让这一行变成红色或任何其他颜色。 这是我的代码:
int count = (tableSummary.getRowCount());
NumberFormat formatter = new DecimalFormat("###,###");
String no = formatter.format(count);
txtNo.setText(no);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String expDateString = sdf.format(cal.getTime()).toString();
for(int i=0; i<=tableSummary.getRowCount()-1; i++){
String nnn= tableSummary.getModel().getValueAt(i, 6).toString();
System.out.println(nnn);
int res = nnn.compareTo(expDateString);
if(res>=0){
System.out.println("if ni " + (res>=0));
}
else{
System.out.println("else ni" + (res>=0));
rowrenderer.setBackground(Color.RED);
}
}
有人能帮我吗?因为这是我界面的一大亮点。 提前感谢!!:)
# 1 楼答案
在下面的链接中,您可以找到想要执行的操作的示例:http://www.roseindia.net/java/example/java/swing/SadingRows.shtml
您必须覆盖
JTable
上的prepareRenderer()
,并在返回的Component
上添加背景色PS:为了将来的参考,如果你能包含更多的代码会更容易。你的
rowrenderer
=)的定义编辑
使用以下方法代替常规的
JTable table = new JTable(model)
声明(如果您想要的不是交替颜色,请更改prepareRenderer
方法中的逻辑):# 2 楼答案
# 3 楼答案
。。。在前面的线程和答案中描述,仅举一个例子
来自代码