1

I have written a table model using AbstractTableModel as below.

class DataTableModel extends AbstractTableModel {

    /** The table. */
    private DataTable table;

    /** The Constant LOGGER. */
    static final Logger LOGGER = Logger.getLogger(DataTableModel.class);

    /**
     * Instantiates a new data table model.
     * 
     * @param ptable
     *            the table
     */
    public DataTableModel(final DataTable ptable) {
        this.table = ptable;
    }

    /**
     * Adds table model listener.
     * 
     * @param arg0
     *            the arg0
     */
    @Override
    public void addTableModelListener(final TableModelListener arg0) {
    }

    /**
     * Gets column class.
     * 
     * @param col
     *            the col
     * @return the column class
     */
    @Override
    public Class<?> getColumnClass(final int col) {
        return table.getColumns().get(col).getTypeClass();

    }

    /**
     * Gets column count.
     * 
     * @return the column count
     */
    @Override
    public int getColumnCount() {
        return table.getColumns().size();
    }

    /**
     * Gets column name by column index.
     * 
     * @param columnIndex
     *            the column index
     * @return the column name
     */
    @Override
    public String getColumnName(final int columnIndex) {
        HashList<ColumnDefinition> columns = table.getColumns();
        String columnName = columns.getColumnName(columnIndex);
        return (columnName + " (" + columns.get(columnName).getType() + ")");
    }

    /**
     * Gets row count.
     * 
     * @return the row count
     */
    @Override
    public int getRowCount() {
        return table.getRowCount();
    }

    /**
     * Gets value at given cell.
     * 
     * @param row
     *            the row
     * @param col
     *            the col
     * @return the value at
     */
    @Override
    public Object getValueAt(final int row, final int col) {
        if (table.getColumns().get(col).getType() == TypeEnum.Boolean) {
            if (table.get(row, col) != null
                    && "true".equalsIgnoreCase(table.get(row, col))) {
                return Boolean.TRUE;
            }

            return Boolean.FALSE;
        } else if (table.getColumns().get(col).getType() == TypeEnum.Double) {
            try {
                String r = this.table.get(row, col);
                return r;
                Object obj = row.get(col);

                if (row == null) {
                    return null;
                }

                return obj;
            } catch (IndexOutOfBoundsException e) {
                return null;
            }
        }

        return table.get(row, col);
    }

    /**
     * Gets whether the given cell is editable.
     * 
     * @param arg0
     *            the arg0
     * @param arg1
     *            the arg1
     * @return true, if is cell editable
     */
    @Override
    public boolean isCellEditable(final int arg0, final int arg1) {
        System.out.println("=========isCellEditable=========");
        return true;
    }

    /**
     * Removes table model listener.
     * 
     * @param arg0
     *            the arg0
     */
    @Override
        public void removeTableModelListener(final TableModelListener arg0) {
    }

    /**
     * Sets given value at given cell.
     * 
     * @param value
     *            the value
     * @param row
     *            the row
     * @param col
     *            the col
     */
    @Override
    public void setValueAt(final Object value, final int row, final int col) {
        if (table.getColumns().get(col).getType() == TypeEnum.Boolean) {
            if (value instanceof Boolean
                    && ((Boolean) value).booleanValue() == Boolean.TRUE) {
                table.setValue(row, col, "true");
                LOGGER.info("true " + value.toString());
            } else {
                LOGGER.info("true " + value.toString());
                table.setValue(row, col, "false");
            }
        } else if(table.getColumns().get(col).getType()==TypeEnum.Double){
            table.setValue(row, col, String.valueOf(value));
        } else {
            if (value != null) {
                table.setValue(row, col, value.toString());
            }
        }

        fireTableCellUpdated(row, col);
    }

when I am adding double values to a table cell it gives an exception

"Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number."

Please help me to solve this. Thank You

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Hasanthi
  • 1,251
  • 3
  • 14
  • 30

1 Answers1

0

The objects you are trying to display don't fit with the format (java.text.Format).

An idea is probably a break point on IllegalArgumentException to see which type of format is used for your Doubles. On Eclipse: "Run >> Add Java Exception Breakpoint..."

Adrien
  • 7
  • 5