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
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.
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.