JTable sorting issue

I have a JTable with columns that contain only numbers. I am using setAutoCreateRowSorter(true) to sort the columns. The number columns by deafault use number comparator.

The issue that rises is when i havean empty cell in the number column, i get aClassCastException(). How can I handle such situations please.

Thanks in advance.

[365 byte] By [faizbasha] at [2007-11-26 20:39:09]
# 1

Working on a deadline for work... ;-)

Hope this helps you out. I ran across something like this the other day.

You might see if you can change your default TableRenderer to fix this for you.

So something like this...

package gui.model;

import java.awt.Component;

import java.util.Date;

import javax.swing.JTable;

import javax.swing.table.DefaultTableCellRenderer;

import java.awt.Color;

import util.AcConfig;

public class AC_TableCellRenderer

extends DefaultTableCellRenderer

{

/**

* Format this based on the default type.

* Just do what the parent does

*

* @param value Object

*/

public void setValue(Object value)

{

if (value instanceof Date)

{

setHorizontalAlignment(CENTER);

super.setValue(AcConfig.GUI_DATE.format(value));

return;

}

super.setValue(value);

}

/**

*

* Returns the default table cell renderer.

*

* @param table the <code>JTable</code>

* @param value the value to assign to the cell at

*<code>[row, column]</code>

* @param isSelected true if cell is selected

* @param hasFocus true if cell has focus

* @param row the row of the cell to render

* @param column the column of the cell to render

* @return the default table cell renderer

*/

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,

int row, int column)

{

// call super for default behavior

super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

if (column == 0 || value.equals("N/A"))

{

setHorizontalAlignment(CENTER);

}

else

{

setHorizontalAlignment(RIGHT);

}

if (value.equals("N/A"))

{

setForeground(Color.RED);

}

else

{

setForeground(Color.BLACK);

}

return this;

}

}

Then when you build your JTable do ...

// put a special renderer on the table for STRINGS and DATES

DefaultTableCellRenderer renderer = new AC_TableCellRenderer();

_allDataTable.setDefaultRenderer(String.class, renderer);

_allDataTable.setDefaultRenderer(Date.class, renderer);

I'm guessing but I'm willing to bet you have an edge condition where you number is a NaN or null and blowing up the cell renderer or cell editor.

Regards,

paducahWildcata at 2007-7-10 1:56:32 > top of Java-index,Desktop,Core GUI APIs...