Discussion:
Float in jTable exactly with two decimal
areeda
2011-08-16 12:59:44 UTC
Permalink
The best way I know of to control exactly how a cell is displayed is to create a custom cell renderer.

See http://download.oracle.com/javase/tutorial/uiswing/components/table.html

Joe
Hitrix
2011-08-16 12:24:12 UTC
Permalink
Hey...

at the moment I'm trying to insert some €-values saved as float values in a MySQL database into a jTable. The binding works fine, but now an other problem occurs:

The values of the float are display not exactly with 2 decimals. For example if a value like 19,90 should be displayed the Table only shows 19,9. How can I force the jTable to display exactly the two decimals?

thanks in advance

Hitrix
Hitrix
2011-08-16 15:53:12 UTC
Permalink
Thanks, you made my day:

My solution:


Code:
new DefaultTableCellRenderer() {

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
DecimalFormat df = new DecimalFormat("#0.00");
if (value != null) {
this.setText(df.format(Double.parseDouble(value.toString())));
}
else {
this.setText("");
}
this.setHorizontalAlignment(RIGHT);
return this;
}

}

Loading...