Click here to Skip to main content
14,364,458 members
Rate this:
Please Sign up or sign in to vote.
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).

<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){

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.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100