Hi,
I am writting code for a Jtable. Now in this Jtable I list file files based on different attributes, including CRC Hash.
Now I have a table that has 6 columns. the data Model extends the Abstract data Model.
In my data Model I have two particular methods for entering and retrieving colours for the columns that match its class.
class AttributeTableCellRenderer extends DefaultTableCellRenderer
{
private Color colour1 = Color.LIGHT_GRAY;
private Color colour2 = Color.PINK;
ResultsModel rm;
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
rm = (ResultsModel) table.getModel();
Attribute a = (Attribute) value;
Component c = super.getTableCellRendererComponent(table, a.getAttribute(), isSelected, false, row, column);
if(value instanceof Attribute)
{
try
{
Attribute b = (Attribute)rm.getValueAt(row - 1, column);
System.out.println("passed rm.getValue");
String s1 = (String) a.getAttribute();
String s2 = (String) b.getAttribute();
if(s1.compareTo(s2) == 0)
{
// System.out.println(s1+ " "+ s2);
c.setBackground(rm.getColour(row-1));
rm.setColour(rm.getColour(row-1));
}
else
{
if(rm.getColour(row-1) != matchColour)
{
c.setBackground(matchColour);
rm.setColour(matchColour);
}
}
return c;
}
catch(Exception e)
{
System.out.println(e);
c = super.getTableCellRendererComponent(table, a.getAttribute(), isSelected, false, row, column);
c.setBackground(colour1);
rm.setColour(colour1);
table.repaint();
return c;
}
}
c = super.getTableCellRendererComponent(table, a.getAttribute(), isSelected, false, row, column);
c.setBackground(colour2);
rm.setColour(colour2);
return c;
}
}
The methods for setting and getting the colours in the table model are as follows:
public void setColour(Color c)
{
colourList.add(c);
//System.out.println(colourList.size());
}
public Color getColour(int i)
{
return (Color)colourList.get(i);
}
The errors or part of the results are as follows and depend on the number of files I find to enter into the table.
Sorry I do not know if I should wrap the error in the ICode tags.
java.lang.ArrayIndexOutOfBoundsException: -1
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
java.lang.ArrayIndexOutOfBoundsException: -1
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
passed rm.getValue
java.lang.ArrayIndexOutOfBoundsException: -1
I understand the reason for the exception as there is nothing initially in the rm.getValueAt() so hence the try and catch. It works as expected but then some more Array Index out of bounds exceptions appear due to the fact the renderer works when you actually see the cells. E.g when you scroll down the table as the other cells come into the view, only then will the renderer execute to paint them. This make is a problem as the exceptions crop up. Is there a way around this?