Hi guys,
I would like to change the color of Jtable cell when it is clicked by mouse (like change it when click it once, change back when click it again).
<pre>private void jTableMouseReleased(java.awt.event.MouseEvent evt) {
int row = jTable.rowAtPoint(evt.getPoint());
int col = jTable.columnAtPoint(evt.getPoint());
if (row >= 0 && col >= 0) {
Component c = jTable.getCellRenderer(row, col).getTableCellRendererComponent(jTable, null, false, true, row, col);
if(c.getBackground() == Color.RED){
c.setBackground(Color.BLUE);
}
else{
c.setBackground(Color.RED);
}
}
}
But it does not work for a single cell, it keeps changing the colors of many cells. Also if I click on the same cell several times, nothing happens.
Can anybody help me, please?
Thanks in advance.
What I have tried:
I checked out examples on the net, they usually suggest using a custom renderer, but I am not sure if I need it for this.