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();
}}
此处订单Id
为String
,Quantity
为int
,price
为float
值。
即使我将列类定义为Float
,它在打印时也不会显示在表中。它只显示一个没有任何浮点的整数(10.61 shows as
10`)。
当我将其设为float
而不是Float
时,它显示为10.0
而不是10.6
。
任何解决方案?