[英]conditional jtable background row
Hi I have a problem I have a list of Persnäs in a table, also have a list of bad people at a vector and I want to load jtable, poor people are marked in red tabla= table conteo= count 嗨,我有一个问题,我在表中有一个Persnäs列表,在向量中也有一个坏人列表,我想加载jtable,穷人用红色tabla = table conteo = count标记
the problem of the code that I have is that only one match brand eventhough it has several 我拥有的代码问题是,尽管有多个匹配品牌,但只有一个匹配品牌
tabla.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
try {
String sql;
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String bdd = "616c756d6e6f";
String par = "create=true";
String conexion = "jdbc:derby:" + bdd + ";" + par;
Connection con = DriverManager.getConnection(conexion);
sql="select matricula from bajas";
PreparedStatement st = con.prepareStatement(sql);
ResultSet rs= st.executeQuery();
Vector<String> vec = new Vector<String>();
while(rs.next())
{
vec.addElement(rs.getString("matricula"));
}
con.close();
for(int x=0;x<tabla.getRowCount();x++)
{
int conteo=0;
for(String valor:vec)
{
if(valor.equals(tabla.getValueAt(x, 0)))
{
conteo++;
}
}
if(conteo!=0)
{
final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
c.setBackground(row ==x ? Color.RED:null);
return c;
}
}
} catch (SQLException ex) {
Logger.getLogger(InicioAlumnos.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
This... 这个...
for(int x=0;x<tabla.getRowCount();x++)
{
int conteo=0;
for(String valor:vec)
{
if(valor.equals(tabla.getValueAt(x, 0)))
{
conteo++;
}
}
Doesn't make sense. 没道理 A
TableCellRenderer
is responsible for a individual cell, not the entire table. TableCellRenderer
负责单个单元,而不是整个表。
Assuming you only want to mark the row that matches your blacklist, you should be using... 假设您只想标记与黑名单匹配的行,则应使用...
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (vec.contains(table.getValueAt(row, 0)) {
setBackground(Color.RED);
}
return this;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.