Reputation: 1
I have a jtable
I populated from a database , but I want to enable or gray out some of this jtable lines ( lines that exist in another table of the same database) for the user cannot check the checkbox
of these lines, but the rest of the lines ( lines that do not exist in this table ) can always be checked.
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>
}
}
Upvotes: 0
Views: 514
Reputation: 1905
Coloring a row can be perform using a modified TableCellRenderer
. I have created a customized TableCellRenderer
as follows.
ColorTableRenderer.java
It can add rows to be mark as gray, and clear all marked rows.
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;
}
}
Using the ColorTableRenderer
Set the ColorTableRenderer
to the table using one of following methods.
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);
Considering your code, you can add following modifications to make selected row color.
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);
}
}
This is my tested screen-shot
Upvotes: 0
Reputation: 389
sir, you can create your checkboxes in an array for accessing them easier.
JCheckBox [] checkboxes= new JCheckBox[WIDTH];
if you found 2nd index to be duplicate you can simply disable the 2nd checkbox in your array
checkboxes[1].setEnabled(false);
Upvotes: 0