Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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).

Java
<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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900