0
public class BuySideTableModel extends AbstractTableModel {

private ArrayList<Order> m_orders;
private String[] m_columnNames={"Order ID", "Quantity", "Price" };
public Class[] m_colTypes={ String.class,Integer.class, Float.class};

public BuySideTableModel(ArrayList<Order> orders) {

    this.m_orders = orders;
}

@Override
public int getRowCount() {
    return m_orders.size();
}

@Override
public int getColumnCount() {
    return m_columnNames.length;
}

@Override
public String getColumnName(int col) {
    return m_columnNames[col];
}

@Override
public Class getColumnClass(int col) {
    return m_colTypes[col];
}

@Override
public Object getValueAt(int rowIndex, int columnIndex ) {
    if (columnIndex == 0) {
        return m_orders.get(rowIndex).getOrderID();
    } else if (columnIndex == 1) {
        return m_orders.get(rowIndex).getQuantity();
    } else {
        DecimalFormat format = new DecimalFormat("#0.00");
        return format.format(new Double(m_orders.get(rowIndex).getPrice()));
    }
}

@Override
public boolean isCellEditable(int row, int col) {
    return false;
}

@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {


if (columnIndex == 0) {
        Order s = m_orders.get(rowIndex);
        s.setOrderID(((Order)value).getOrderID());
        m_orders.remove(rowIndex);
        m_orders.add(rowIndex, s);
  }
  else if (columnIndex ==1) {
        Order s = m_orders.get(rowIndex);
        s.setQuantity(((Order)value).getQuantity());
        m_orders.remove(rowIndex);
        m_orders.add(rowIndex, s);
  }
  else {
        Order s = m_orders.get(rowIndex);
        s.setPrice(((Order) value).getPrice());
        m_orders.remove(rowIndex);
        m_orders.add(rowIndex, s);
    }
    fireTableCellUpdated(rowIndex, columnIndex);
}
public void updateTable(ArrayList<Order> order) {
    this.m_orders = order;
    fireTableDataChanged();
}}

Here Order Id is a String, Quantity is an int and price is a float value.

Even though I defined the column class as Float, it doesn't show in the table when it is printed. It only shows an integer without any floating points (10.61 shows as10`).

When I put it as float instead of Float it shows like 10.0 instead of 10.6.

Any solutions?

Lasitha Konara
  • 1,111
  • 3
  • 12
  • 19
  • 1
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jul 22 '15 at 07:30
  • Seems to work just fine for me... – MadProgrammer Jul 22 '15 at 07:36
  • The default table cell renderer sets the label text using `toString()` and should work. Your model might be converting the `float` to an `integer` somewhere. Post your model code, if you are not able to figure it out. The `getColumnClass` modifies the behaviour of `DefaultTableCellEditor`. – Dakshinamurthy Karra Jul 22 '15 at 07:37

0 Answers0