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?

Yes. The initial try catch handles this as there is no row the first time the table comes into existence. There after it works as normal until the last row that can be seen within the scrollpane which is 11 rows when it tries to get to twelves the rows resets back to 0 and thus the arrayIndexOutofBounds exception. which again is caught but this time it enters the default colour colour1 when infact the value in the 12th row is the same as the 11th and thus the colour needs to be matchcolour.

Can You show or post Attribute and ResultsModel classes to make a deeper analyse ?
quuba

Yes I shall do so when I get home. Also I have system.out.println(row); in the renderer class and the value of row ascends as normal by 1 but when it gets to 11 it then reverts back to 0, throwing the ArrayindexOutofBounds exception throwing into disarray my intentions.

Results model class

class ResultsModel extends AbstractTableModel implements Comparator {

        private List data;
        private List plugins;
        private List markedList;
        private List colours;
        private boolean isMode2;
        private boolean isAdvanced;
        private boolean markAll = false;
        private int sorting;
        
        boolean isAdvancedMode() {
            return isAdvanced;
        }

        void updateUIMode(boolean isAdvanced) {
            if (isAdvanced) {
                lastColumnCount = 2;
            } else {
                lastColumnCount = 1;
            }
            if (this.isAdvanced != isAdvanced) {
                this.isAdvanced = isAdvanced;
                resetColumns();
            }
        }

        private void resetColumns() {
            fireTableStructureChanged();

            TableColumnModel tcm = tblResults.getColumnModel();
            TableColumn tc = tcm.getColumn(0);
            tc.setMaxWidth(24);
            tc.setResizable(false);
            if (isAdvanced) {
                tc = tcm.getColumn(1 + firstColumnCount + attributeColumnCount);
                tc.setMaxWidth(18);
                tc.setResizable(false);
            }


        }

        void mark() {
            for (int i = 0; i < markedList.size(); i++) {
                markedList.set(i, new Boolean(!markAll));
            }
            markAll = !markAll;
        }

        void sort(int col) {
            sorting = col - 2;
            Collections.sort(data, this);
            fireTableRowsUpdated(0, data.size());
        }

        public ResultsModel() {
            data = new ArrayList();
            markedList = new ArrayList();
            plugins = new ArrayList();
            colourList = new ArrayList();
            
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex == 0 ? true : false;
        }

        public Class getColumnClass(int columnIndex) {
            if (columnIndex == 0) 
            {
                return Boolean.class;
            } 
            else if (columnIndex == 1) 
            {
                return AttributedFile.class;
            } 
            else if(columnIndex == 2)
            {
                return String.class;
            }
            else if(columnIndex == 3)
            {
                return String.class;
            }
            else if(columnIndex == firstColumnCount+attributeColumnCount) 
            {
		return ImageIcon.class;
            }
            else if(columnIndex == 1+firstColumnCount+attributeColumnCount) 
            {
		return  ImageIcon.class;	
            }
		//return Attribute.class;
            
            //else if(columnIndex < firstColumnCount + attributeColumnCount) 
            //{
                return Attribute.class;
            //}
            
            //return String.class;
        }

        public String getColumnName(int columnIndex) 
        {
             if(columnIndex == 0) {
				return "{}";
			} 
             else if(columnIndex == 1) 
             {
                    return "File";
             } 
              else if(columnIndex == 2) 
             {
                    return "Path";
             } 
             else if(columnIndex == 3)
             {
                 return "Last-Modified";
             }
             else if(columnIndex < firstColumnCount + attributeColumnCount) 
             {
		Object p = plugins.get(columnIndex - firstColumnCount);
		if(p instanceof ProcessingPlugin) 
                {
                    return ((ProcessingPlugin) p).getAttributeName();
		} 
                else 
                {
                    return "";
		}
             }
             else
             {
                return "";
             }  
           
        }

        public int getRowCount() {
            return data.size();
        }
        private int attributeColumnCount = 0;
        private int firstColumnCount = 3;
        private int lastColumnCount = 1;

        public int getColumnCount() {
            return firstColumnCount + attributeColumnCount + lastColumnCount;
        }
       
        public List getColourList()
        {
            return colourList;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0) 
            {
                return markedList.get(rowIndex);
            } 
            else if(columnIndex == 1) 
            {
                return ((AttributedFile)data.get(rowIndex)).getFile();
            }
             else if (columnIndex == 2) 
            {
                return ((AttributedFile) data.get(rowIndex)).getPath();
            } 
            else if(columnIndex == 3)
            {
                return ((AttributedFile) data.get(rowIndex)).getDate();
        
            }
            else if(columnIndex == attributeColumnCount + firstColumnCount) 
            {
		return iconReveal;
            } 
            else if(columnIndex == 1+attributeColumnCount + firstColumnCount) 
            {
		return iconGear;
            }
            else if(columnIndex < firstColumnCount + attributeColumnCount) 
            {
                ProcessingPlugin plugin = (ProcessingPlugin) plugins.get(columnIndex - firstColumnCount);
               
                AttributedFile file = (AttributedFile) data.get(rowIndex);
                
		return file.getAttributes().get(plugin);
            }
            else
                return "?";
            
        }
            
        

        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            if (columnIndex == 0 && aValue instanceof Boolean) {
                markedList.set(rowIndex, aValue);
            }
        }

        void setResults(List results) {
            data = results;
           // ProcessingPlugin p = (ProcessingPlugin) plugins.get(columnIndex)
            markedList = new ArrayList(results.size());
            for (int i = 0; i < results.size(); i++) {
                markedList.add(new Boolean(false));
            }
            colourScheme(data);
            resetColumns();
        }
        public List getReults()
        {
            //System.out.println(data.size());
            return data;
            
        }
        public int returnsize()
        {
            return data.size();
        }
         public void setColour(Color c)
        {
            colourList.add(c);
            //System.out.println(colourList.size());     
        }
        public Color getColour(int i)
        {
            return (Color)colourList.get(i);
        }
        
        public int getPlugins()
        {
            return plugins.size();
        }
        void setUsedPlugins(List usedPlugins) {
            plugins = usedPlugins;
            attributeColumnCount = plugins.size();
            System.out.println(attributeColumnCount + "   plugins count");
        }

        void resultsWillBeInMode2(boolean mode2) {
            isMode2 = mode2;
        }

        public int compare(Object o1, Object o2) {
            if (o1 == null && o2 == null) {
                return 0;
            }
            if (o1 == null) {
                return 1;
            }
            if (o2 == null) {
                return -1;
            }
            AttributedFile a = (AttributedFile) o1;
            AttributedFile b = (AttributedFile) o2;
            ProcessingPlugin p = (ProcessingPlugin) plugins.get(sorting);
            Object aa = a.getAttributes().get(p);
            Object bb = b.getAttributes().get(p);
            if (aa == null && bb == null) {
                return 0;
            }
            if (aa == null || aa.equals("")) {
                return 1;
            }
            if (bb == null || bb.equals("")) {
                return -1;
            }
            if (aa.equals("") && bb.equals("")) {
                return 0;
            //System.out.println(aa+"::"+aa.getClass());
            }
            try {
                return ((Attribute) aa).getAttribute().compareTo(bb);
            } catch (Exception exception) {
            }
            return 1;
        }
    }

Attribute class

public class Attribute {

	private Comparable attribute;
	private boolean highlighted;

    public Comparable getAttribute() {
        return attribute;
    }

    public boolean isHighlighted() 
    {
        return highlighted;
    }

    public void setHighlighted(boolean highlighted) 
    {
        this.highlighted = highlighted;
    }
	
    public Attribute(Comparable attribute) 
    {
		this.attribute = attribute;
    }
   

}

hello Grub
My general remarks:
- use "generic" introduced in Tiger java version
- use additional compiler option -Xlint:unchecked Programming stay easier.

I try apply this mechanism in Your code:

class ResultsModel extends AbstractTableModel implements Comparator<AttributedFile> {

    private List<AttributedFile> data;
    private List<ProcessingPlugin> plugins;
    private List<Boolean> markedList;
    private List<Color> colours;
    private boolean isMode2;
    private boolean isAdvanced;
    private boolean markAll = false;
    private int sorting;
...

For this moment i need additional information about declaration type:

public class Attribute {

    private Comparable attribute;
    private boolean highlighted;

    public Attribute(Comparable attribute) {
        this.attribute = attribute;
    }

    public Comparable getAttribute() {
        return attribute;
    }

    public boolean isHighlighted() {
        return highlighted;
    }

    public void setHighlighted(boolean highlighted) {
        this.highlighted = highlighted;
    }

    public <?> get(ProcessingPlugin plugin) {
        // throw new UnsupportedOperationException("Not yet implemented");       
    }
}

Without this info i can't resolve problem in function public int compare(AttributedFile a, AttributedFile b) {...} and go forward
quuba

This question has already been answered. Start a new discussion instead.