嗨我有一个问题我在表中有一个Persnäs列表,还有一个向量中的坏人列表,我想加载jtable,穷人用红色标记 tabla = table conteo = count
我所拥有的代码问题是只有一个匹配品牌,尽管它有几个
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;
}
答案 0 :(得分:0)
此...
for(int x=0;x<tabla.getRowCount();x++)
{
int conteo=0;
for(String valor:vec)
{
if(valor.equals(tabla.getValueAt(x, 0)))
{
conteo++;
}
}
没有意义。 TableCellRenderer
负责单个单元格,而不是整个表格。
假设您只想标记与您的黑名单匹配的行,您应该使用...
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (vec.contains(table.getValueAt(row, 0)) {
setBackground(Color.RED);
}
return this;