I have an array of ints called sponsorIndexArr which contains the indices of the cells that I want to change the color of in a table (I also want to make that cell unselectable). The table is one column so I only need the row index of the cell.
Here is some relevant code:
// Configure sponsor table
sponsorstableModel = new DefaultTableModel(sponsorsTableList, new String[]{"Sponsors"}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
sponsorsTable = new JTable(sponsorstableModel);
sponsorsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sponsorsTable.addMouseListener(this);
sponsorsTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
for (int entry : sponsorIndexArr) {
System.out.println(entry + " " + row);
if (entry == row) {
System.out.println("HERE");
this.setBackground(Color.CYAN);
this.setEnabled(false);
} else {
setBackground(null);
this.setEnabled(true);
}
}
return this;
}
});
The program is printing "HERE" in the correct places. However, what is happening is that only the cell with the last index of sponsorIndexArr is changing colors. When I get rid of setBackground(null)
then every cell becomes cyan.
Also when I select any of the other cells the background covers the text. When I get rid of this.setEnabled(true)
then I don't have this problem, but then every cell is disabled (text goes gray).