I had a simple cell renderer that takes in two integers. The first integer corresponds to the colour and the second to the row which needs to be coloured in. However, I soon realised it had several problems but the major one being that the cell renderer continued colour rows past the specified row.
I thought it worked, but the else clause (for the if row = inputedRow), was setting the cell colour to white. I removed this line, but the empty else clause continued to fill in all rows below the specified one. To fix this I used:
Color originalColour=x.getBackground();
x.setBackground(originalColour);
I had some success with this on my testing project, but once again the if loop's (the only change in the code) else clause made this not work.
Could somebody point out what the problem is and how to fix it?
My renderer code (sorry for the inefficient if-else stack, but I replaced the more efficient method with this for error checking). Also the boolean was just so I could reset the table colour to black to check if it was receiving the old cell colour.
class Renderer extends DefaultTableCellRenderer{
private int colourNo;
private int rowNo;
private boolean reset;
public Renderer(int colourNo, int rowNo, boolean reset){
this.colourNo=colourNo;
this.rowNo=rowNo;
this.reset=reset;
}
@Override
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
JLabel x = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
if(reset==false){
if(row==rowNo){
if(colourNo==1){
x.setBackground(Color.GREEN);
}else{
if(colourNo==2){
x.setBackground(Color.MAGENTA);
}else{
if(colourNo==3){
x.setBackground(Color.ORANGE);
}else{
if(colourNo==4){
x.setBackground(Color.BLUE);
}else{
if(colourNo==5){
x.setBackground(Color.YELLOW);
}else{
if(colourNo>5){
x.setBackground(Color.RED);
}else{
x.setBackground(Color.WHITE);
}
}
}
}
}
}
}else{
Color originalColour=x.getBackground();
x.setBackground(originalColour);
}
}else{
x.setBackground(Color.BLACK);
}
return x;
}
}
If somebody could also help with the for loops that call the renderer, as it sometimes works, and sometimes replace the previous iterations render. However, regardless of how and how many times time the renderer is called, the main problem above is still present.
for(int i =0;i<rows;i++){
for(int j=0;j<columns;j++){
Table.getColumnModel().getColumn(i).setCellRenderer(new Renderer(displayStarred[i],j,false));
//(displaystarred stores the colour index)
}
}
Aucun commentaire:
Enregistrer un commentaire