Java JTable启用某些复选框列

Java JTable启用某些复选框列,java,swing,jtable,jcheckbox,Java,Swing,Jtable,Jcheckbox,我有一个从数据库填充的jtable,但我想启用或灰显其中一些jtable行(存在于同一数据库的另一个表中的行),因为用户无法选中这些行的复选框,但可以始终选中其余行(此表中不存在的行) for (int m = 0; m < tb_doublon.getRowCount(); m++) { Statement statdouble=null; ResultSet rsdouble=null; //I get the value of the cell of t

我有一个从数据库填充的
jtable
,但我想启用或灰显其中一些jtable行(存在于同一数据库的另一个表中的行),因为用户无法选中这些行的
复选框,但可以始终选中其余行(此表中不存在的行)

for (int m = 0; m < tb_doublon.getRowCount(); m++) {
    Statement statdouble=null;  
    ResultSet rsdouble=null;

    //I get the value of the cell of the column 1 :id, line : i
    String id = (String)tb_doublon.getValueAt(m, 1);
    String cli = (String)tb_doublon.getValueAt(m, 2);

    //i browse the other table to enable or gray out the lines existing in that table with th id
    String doubleexistant ="select * from doublon where id='"+id+"' and cli='"+cli+"'" ;
    statdouble = conn.createStatement();
    rsdouble  = statdouble.executeQuery(doubleexistant);
    while (rsdouble.next()) {
      //i think this is here that i must enable or gray out the lines but i don't know how !!!!<br>             
    }
}
for(int m=0;m
}
}

先生,您可以在数组中创建复选框,以便更轻松地访问它们

JCheckBox [] checkboxes= new JCheckBox[WIDTH];
如果发现第二个索引重复,只需在数组中禁用第二个复选框即可

checkboxes[1].setEnabled(false);

可以使用修改的
TableCellRenderer
对行进行着色。我创建了一个定制的
TableCellRenderer
,如下所示

ColorTableRenderer.java

它可以添加要标记为灰色的行,并清除所有标记的行

public class ColorTableRenderer extends DefaultTableCellRenderer {

    //contains row indexes which need to color
    private final List<Integer> colorIndexes = new ArrayList<>();

    //add new index to show as color
    public void addColorIndex(Integer index) {
        colorIndexes.add(index);
    }

    //clear all color indexes
    public void clearColorIndexes() {
        colorIndexes.clear();
    }

    private boolean isColorIndex(Integer index) {
        return colorIndexes.contains(index);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        if (isColorIndex(row)) {//check if marked as colored
            component.setBackground(Color.LIGHT_GRAY);//highlight color
        } else {
            component.setBackground(Color.WHITE);//other color
        }

        return component;
    }
}
考虑到您的代码,您可以添加以下修改以使选定的行具有颜色

    renderer.clearColorIndexes();
    for (int m = 0; m < tb_doublon.getRowCount(); m++) {
        Statement statdouble = null;
        ResultSet rsdouble = null;

        //I get the value of the cell of the column 1 :id, line : i
        String id = (String) tb_doublon.getValueAt(m, 1);
        String cli = (String) tb_doublon.getValueAt(m, 2);

        //i browse the other table to enable or gray out the lines existing in that table with th id
        String doubleexistant = "select * from doublon where id='" + id + "' and cli='" + cli + "'";
        statdouble = conn.createStatement();
        rsdouble = statdouble.executeQuery(doubleexistant);
        while (rsdouble.next()) {
            renderer.addColorIndex(m);
        }
    }
renderer.clearColorIndexes();
for(int m=0;m
这是我测试过的屏幕截图


您必须创建一个定制的表格模型。请添加有关表格模型的信息。此外,我不建议在循环中运行sql语句。你可以用一种适当的方式来管理它。
    renderer.clearColorIndexes();
    for (int m = 0; m < tb_doublon.getRowCount(); m++) {
        Statement statdouble = null;
        ResultSet rsdouble = null;

        //I get the value of the cell of the column 1 :id, line : i
        String id = (String) tb_doublon.getValueAt(m, 1);
        String cli = (String) tb_doublon.getValueAt(m, 2);

        //i browse the other table to enable or gray out the lines existing in that table with th id
        String doubleexistant = "select * from doublon where id='" + id + "' and cli='" + cli + "'";
        statdouble = conn.createStatement();
        rsdouble = statdouble.executeQuery(doubleexistant);
        while (rsdouble.next()) {
            renderer.addColorIndex(m);
        }
    }