Re: formatting floats in a table
- From: Roland <roland@xxxxxxxxx>
- Date: Mon, 27 Jun 2005 14:23:56 +0200
On 27-6-2005 8:42, farseer wrote:
Hi, i have a table whose columns can contain either Strings, Integers, or Floats. I also allow sorting by clicking on the header of a column.
my question is, i need to display only two decimal places for columns that display floats. How can i do this and still maintain accurate sorting for those columns? Currently, it seems most of the formating functions return Strings, and that may cause the sorting to be incorrect for columns that are suppose to display numbers.
Also i am trying to do this generically using a custom TableCellRenderer class i have written. This renderer has a member variable called "formatString". Basically, when the JTable is initalized, i read some config info, one of which tells me what columns need formatting and how they should be formatted. I then instantiate the TableCellRenderer class, assign the formatString (if required), then call TableColumn.setCellRenderer. Now i just need to know how i can apply that formatString to the Value and still maintain the underlying class type of Value so that i can be properly sorted....
It's not clear to me how you do the sorting. I'm assuming that you are using your own TableModel that takes care of this. Then, make sure that your TableModel (if you have one) returns the correct Class for each column in the getColumnClass method (i.e. Float.class for the Float columns). If this is the case then you can set the default renderer of your table for Float.class to be your TableCellRenderer:
YourTableModel yourTableModel = ...
YourCellRenderer yourCellRenderer = ...
JTable yourTable = new JTable(yourTableModel);
yourTable.setDefaultRenderer(Float.class, yourCellRenderer);
Otherwise (or alternatively) you can specify a TableCellRenderer for each column separately:
yourTable.getColumnModel()
.getColumn(1).setCellRenderer(yourCellRenderer);
Your TableCellRenderer could use a DecimalFormat instance, so it can format Float values to two decimal places. The code below shows a complete example:
import java.awt.BorderLayout; import java.text.*; import javax.swing.*; import javax.swing.table.*;
public class TableExample { public static void main(String[] args) { Object[][] rowData = {
{new Float(0.0),new Float(0.1),new Integer( 2),"[0,3]"},
{new Float(1.0),new Float(1.1),new Integer(12),"[1,3]"},
{new Float(2.0),new Float(2.1),new Integer(22),"[2,3]"}};
Object[] colNames = { "A", "B", "C", "D" }; YourTableModel yourTableModel =
new YourTableModel(rowData, colNames);
JTable yourTable = new JTable(yourTableModel);
yourTable.setDefaultRenderer(Float.class,
new YourCellRenderer("0.0"));
yourTable.getColumnModel().getColumn(1).setCellRenderer(
new YourCellRenderer("0.00")); JFrame app = new JFrame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.getContentPane().setLayout(new BorderLayout());
app.getContentPane().add(new JScrollPane(yourTable),
BorderLayout.CENTER);
app.setSize(400, 300);
app.setLocationRelativeTo(null);
app.setVisible(true);
}
}class YourCellRenderer extends DefaultTableCellRenderer {
private final NumberFormat formatter; public YourCellRenderer() {
this.formatter = NumberFormat.getNumberInstance();
setHorizontalAlignment(JLabel.RIGHT);
} public YourCellRenderer(String format) {
if (format == null) {
throw new IllegalArgumentException("format cannot be null");
}
this.formatter = new DecimalFormat(format);
setHorizontalAlignment(JLabel.RIGHT);
} protected void setValue(Object value) {
if (value instanceof Number) {
value = formatter.format(value);
}
super.setValue(value);
}
}class YourTableModel extends DefaultTableModel {
public YourTableModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
} public Class getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return Float.class;
case 1:
return Float.class;
case 2:
return Integer.class;
case 3:
return String.class;
default:
return Object.class;
}
}
}-- Regards,
Roland de Ruiter ` ___ ___ `/__/ w_/ /__/ / \ /_/ / \ .
- Follow-Ups:
- Re: formatting floats in a table
- From: farseer
- Re: formatting floats in a table
- References:
- formatting floats in a table
- From: farseer
- formatting floats in a table
- Prev by Date: Re: How to handle exceptions in constructor?
- Next by Date: remove redundency in list of pairs
- Previous by thread: formatting floats in a table
- Next by thread: Re: formatting floats in a table
- Index(es):