简体   繁体   中英

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

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;
}
  1. Don't do anything in your renderers that could be time consuming (this is pretty good rule for GUI's generally). EACH cell that the renderer is responsible for could call this renderer (have 1000 lines, expect to be hit 1000 times). Instead, pre-load you "black" list and use the cached values. If you need to, provide a means by which the list can reloaded
  2. Always return a value from the cell renderers, failing to do so may cause other problems.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM