在netbeans中着色jtable行 [英] Colouring jtable row in netbeans
问题描述
我是java的新手。我想要做的是创建一个显示对象列表的表。我想要的是根据对象成员的值为JTable中的特定行赋予颜色。我看到很多选项,比如使用TableCellRender和所有。我也尝试过它们。但问题是我使用Netbeans IDE,所以我不是通过代码创建表。有人可以帮我改变NetBeans定义表格的行颜色吗?
I'm new to java. What i am trying to do is to create a table that shows a list of objects. What i want is to give color for specific rows in the JTable based upon the value of the member of an object. I saw a lot of option like using "TableCellRender" and all.I have tried them too. But the problem is that am using the Netbeans IDE so that i am not creating the table by code. Can some one please help me to change row color where the table is defined by the NetBeans??
提前致谢。
推荐答案
您可以使用 DefaultTableCellRenderer
从 JTable
为替换行着色。
You can use DefaultTableCellRenderer
to color alternate row from JTable
.
table.setDefaultRenderer(Object.class, new TableCellRenderer(){
private DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(isSelected){
c.setBackground(Color.YELLOW);
}else{
if (row%2 == 0){
c.setBackground(Color.WHITE);
}
else {
c.setBackground(Color.LIGHT_GRAY);
} }
//Add below code here
return c;
}
});
如果你想使用特定行的值为行添色,那么你可以使用这样的东西。将这些行添加到上面
If you want to color your row using the value of a particular row then you can use something like this. Add these line to above
if(table.getColumnModel().getColumn(column).getIdentifier().equals("Status")){//Here `Status` is column name
if(value.toString().equals("OK")){//Here `OK` is the value of row
c.setBackground(Color.GREEN);
}
}
这篇关于在netbeans中着色jtable行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!