简体   繁体   English

有条件的jtable背景行

[英]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;
}
  1. Don't do anything in your renderers that could be time consuming (this is pretty good rule for GUI's generally). 不要在渲染器中做任何可能很耗时的事情(这对于GUI来说通常是很好的规则)。 EACH cell that the renderer is responsible for could call this renderer (have 1000 lines, expect to be hit 1000 times). 渲染器负责的每个单元都可以调用此渲染器(有1000行,预计将被击中1000次)。 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. 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.

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