JTable prepareRenderer changes all cell colors
Hi
Im trying to change row colors in a JTable but the whole table changes to the same color. Only if I drag and highlight the whole table, the Specific rows im trying to change, show the correct color. I dont understand this, am I forgetting something?
public vRows is a Vector set from the main class and mainTable is a customTable
can I set vRows from the main Class?
/************************************************** *********************/
this is from the main Class in "button_clicked"
Vector vColor = new Vector();
vColor.add(1);
vColor.add(2);
vColor.add(6);
vColor.add(7);
mainTable.vRows = vColor;
mainTable.setModel(model);
mainTable.repaint();
/************************************************** */
public class CustomTable extends JTable {
public Vector vRows;
public CustomTable(int numRows, int numColumns) {
super(numRows, numColumns);
}
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column){
Component comp = super.prepareRenderer(renderer, row, column);
int i = 0;
while (vRows.size() > (i)){
if (row == vRows.get(i)){
comp.setBackground(Color.LIGHT_GRAY);
}
i++;
}
repaint();
return comp;
}
/************************************************** ********************/
Thanks
Brett
Re: JTable prepareRenderer changes all cell colors
Ok, I'm just brain-storming from the API here, so dont take everything I suggest to heart.
Maybe you can take advantage of the setRowSelectionInterval(int index0, int index1) and the setSelectionBackground(Color selectionBackground) methods. To my best guess, you could go through the JTable, selecting rows with the setRowSelectionInterval method and set the color with the setSelctionBackground method.
Try it, might work.
Re: JTable prepareRenderer changes all cell colors
Awesome thank you so much.
It works perfectly.
Brett
Re: JTable prepareRenderer changes all cell colors
Good to know if I ever have to do it in the future.
Re: JTable prepareRenderer changes all cell colors
For what its worth, the reason you are receiving this behavior is because the renderer reuses the components. The solution is to reset the color in the prepareRenderer method
Code java:
if (row == vRows.get(i)){
comp.setBackground(Color.LIGHT_GRAY);
}else{
comp.setBackground(Color.WHITE);//or whatever your default color is
}
Re: JTable prepareRenderer changes all cell colors
Hi All
Thanks for the help, now I have another problem.
when I use the setRowSelectionInterval method the row colors are correct but when
I use the getSelectedRow method to return the Row clicked on in the JTable it always returns the
last row that I change the color of using setRowSelectionInterval method.
So setRowSelectionInterval works to change colors of specific rows but returns the wrong row when calling getSelectedRow().
I've also tried removing the RowSelectionInterval with removeRowSelectionInterval method, after I change the row colors but then the getSelectedRow returns -1
Hi Copeg I have tried
if (row == vRows.get(i)){
comp.setBackground(Color.LIGHT_GRAY);
}else{
comp.setBackground(Color.WHITE);//or whatever your default color is
}
but this also only changes the color of the last row I change in the table eg. the last number in the Vector
although then the getSelectedRow method works.
Any guidance.
Thank you
Brett
Re: JTable prepareRenderer changes all cell colors
I tried using the rowAtPoint(evt.getPoint()) method to get the row number and it seems to have worked but I don't know if that's a good way to do it?
Thanks for all the help
Brett
Re: JTable prepareRenderer changes all cell colors
try adding setRowSelectionAllowed(true);
I doubt it will do anything, but maybe it will. You never know.
Re: JTable prepareRenderer changes all cell colors
Quote:
Originally Posted by
BrettStuart
Hi Copeg I have tried
if (row == vRows.get(i)){
comp.setBackground(Color.LIGHT_GRAY);
}else{
comp.setBackground(Color.WHITE);//or whatever your default color is
}
but this also only changes the color of the last row I change in the table eg. the last number in the Vector
although then the getSelectedRow method works.
This may be due to the way you are iterating through your Vector. Try something like
Code java:
if ( vRows.contains(row) ){
comp.setBackground(Color.LIGHT_GRAY);
}else{
comp.setBackground(Color.WHITE);
}
Although after more thought, if you are just wanting to alter the selection color and not the basic rendering color, use the setSelectionBackground already discussed and don't override the prepareRenderer method.
If you wish to receive all the selected rows, call getSelectedRows() which returns an array of all the selected rows, rather than the last on selected.
Re: JTable prepareRenderer changes all cell colors
Hi guys
if ( vRows.contains(row) ){ ... }, sorted the problem.
Thank You so much for the help.
Brett