根据数据库的值改变 JTable 行的颜色 [英] Change Color of JTable Row Based on the Value of the Database
本文介绍了根据数据库的值改变 JTable 行的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
tblApplicant = new javax.swing.JTable(){
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
// Alternate row color
String value = (String) tblApplicant.getValueAt(row, 4);
if (value == "Single" && !isRowSelected(row))
c.setBackground(Color.LIGHT_GRAY);
return c;
}
};
这是我的新代码,我试图获取第 4 列的值,如果背景发生变化,则为单列值.但这不起作用
This is my New Codes im trying to get the value of column 4 and equal it to single if its true the background is change. but this is not working
推荐答案
重写 JTable
的 prepareRender(...)
方法允许您为基于其中一列中的值的整行.
Overriding the prepareRender(...)
method of the JTable
allows your to customize rendering for the entire row based on a value in one of the columns.
基本逻辑类似于:
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
// Color row based on a cell value
if (!isRowSelected(row))
{
c.setBackground(getBackground()); // set default background
int modelRow = convertRowIndexToModel(row);
String value = (String)getModel().getValueAt(modelRow, ???);
if ("Single".equals(value)) c.setBackground(Color.GREEN);
}
return c;
}
查看表格行渲染了解更多信息和工作示例.
Check out Table Row Rendering for more information and working examples.
这篇关于根据数据库的值改变 JTable 行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文