Jtable cell selection and highlighting | Sololearn: Learn to code for FREE!
0

Jtable cell selection and highlighting

Hi, I have an attached code, it working but with a little issue. I'm able to highlight a cell with blue color. What I want is after highlighting a single cell with a blue color I want to be able to un-highlight the same cell by clicking it again. For example when I highlight cell 1, it will be highlighted with blue but when I click it again it won't be un-highlighted unless I click another cell then click cell 1 again. How can I fix this problem. In the code I'm only testing cell 1 to 4. https://code.sololearn.com/cQ1cVEjne87q/?ref=app

31st Jan 2023, 7:32 AM
Sibusiso Mbambo
7 Answers
+ 2
U can set condition take one bool value and do something like this bool isHighlight= false If (isHighlight) Set background blue; isHighlight = true else Set background white isHighlight = false
31st Jan 2023, 7:51 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
But here u have tried to add colour on the bases of condition if (value.equals(oneSValue) || value.equals(twoSValue) || value.equals(threeSValue) || value.equals(fourSValue)) {             c.setBackground(Color.BLUE);             c.setForeground(Color.WHITE);           }         else {         c.setBackground(Color.WHITE);         c.setForeground(Color.BLACK);         }        
31st Jan 2023, 8:12 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
On mouse click u have to add condition u made a separate function for background colour right bool isHighlight = false // by default false Button click function () { If (isHighlight) { Call your getTableCellRendererComponent () // set your colour now set isHighlight to true } else { Call getCellRenderComponent () isHighlight is false }
31st Jan 2023, 8:28 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
It won't work because the code uses getTableCellRendererComponent() method
31st Jan 2023, 8:06 AM
Sibusiso Mbambo
0
So how would you suggest I modify the code ?
31st Jan 2023, 8:15 AM
Sibusiso Mbambo
0
I'm not getting it to work
31st Jan 2023, 9:19 AM
Sibusiso Mbambo
0
Hi Oparah anord
31st Jan 2023, 3:20 PM
Sibusiso Mbambo