[英]Change row Background in JTable Java
I have a table in my project called tasktable
. 我的项目中有一个名为
tasktable
的表。 It retrives data from database (oracle). 它从数据库(oracle)中检索数据。 How can I change the color of the row that has the color in the cell ex.
如何更改单元格ex中具有颜色的行的颜色。 (i,8) automatically when I click the refresh button?
(i,8)当我点击刷新按钮时会自动?
I have tried so many times to put that source code on specific row, but it ended up coloring all the table: 我已经尝试过很多次了,以将源代码放在特定的行上,但是最终使所有表着色:
int count;
count = tasktable.getRowCount();
for (int i=0;i<count;i++)
{
rr = new Object ();
rr = tasktable.getModel().getValueAt(i,8);
if(rr.equals("GREEN"))
{
setBackground(Color.GREEN);
}
if(rr.equals("red"))
{
setBackground(Color.red);
}
if(rr.equals("BLUE"))
{
setBackground(Color.BLUE);
}
if(rr.equals("yellow"))
{
setBackground(Color.yellow);
}
if(rr.equals("pink"))
{
setBackground(Color.pink);
}
if(rr.equals(null))
{
setBackground(null);
}
how can help me out upon this problem ? 如何解决这个问题?
将自定义TableCellRenderer添加到您的表。
setBackground()
sets the background color of the JTable
, not the background color of each row or cell. setBackground()
设置JTable
的背景色,而不是每行或单元格的背景色。 You need a TableCellRenderer
, as @Recursed said. 您需要一个
TableCellRenderer
,如@Recursed所述。
If all you're doing is changing row color, subclass your JTable
and override the prepareRenderer
method: 如果您要做的只是更改行颜色,请为
JTable
子类化并重写prepareRenderer
方法:
public Component prepareRenderer(TableCellRenderer renderer,
int row,
int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (row == HIGHLIGHT_ROW) {
c.setBackground(BG_COLOR);
}
return c;
}
Component comp = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col);
String s = table.getModel().getValueAt(row, VALIDATION_COLUMN ).toString();
comp.setForeground(Color.red);
http://www.java-forums.org/awt-swing/541-how-change-color-jtable-row-having-particular-value.html
http://www.java-forums.org/awt-swing/541-how-change-color-jtable-row-having-particular-value.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.