2015-09-25 53 views
0

我有一個jtable我從數據庫填充,但我想啓用或灰色的一些這種jtable行(存在於同一數據庫的另一個表中的行)爲用戶無法檢查checkbox這些行的其餘部分(行中不存在的行)始終可以檢查。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>    
    } 
} 
+0

你必須創建一個定製的表格模型。請添加關於您的表格模型的信息。此外,我不建議在循環內運行sql語句。你可以用適當的方式管理它。 –

回答

0

先生,您可以在數組中創建複選框以便於訪問它們。

JCheckBox [] checkboxes= new JCheckBox[WIDTH]; 

,如果你發現第2個指標被複制,你可以簡單地禁用陣列

checkboxes[1].setEnabled(false); 
0

着色行可以執行使用改進的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; 
    } 
} 

使用ColorTableRenderer

設置ColorTableRenderer使用下列方法之一表。

ColorTableRenderer renderer = new ColorTableRenderer(); 

    //set TableCellRenderer into a specified JTable column class 
    table.setDefaultRenderer(String[].class, renderer); 

    //or, set TableCellRenderer into a specified JTable column 
    table.getColumnModel().getColumn(columnIndex).setCellRenderer(renderer); 

考慮到您的代碼,您可以添加以下修改以使選定的行顏色。

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); 
     } 
    } 

這是我測試的屏幕截圖

Color Table Cell Test

+0

謝謝eveyone的幫助!!!!我會嘗試這些想法! – randavas