簡體   English   中英

在Java中向TableModel添加雙精度值

[英]Adding double values to a TableModel in java

我已經使用AbstractTableModel編寫了一個表模型,如下所示。

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);
    }

當我向表單元格中添加雙精度值時,它給出了一個例外

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

請幫我解決這個問題。 謝謝

您嘗試顯示的對象與格式(java.text.Format)不匹配。

一個想法可能是IllegalArgumentException一個斷點,以查看您的Doubles使用哪種類型的格式。 在Eclipse上:“運行>>添加Java異常斷點...”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM