|
|
|
|
Author |
Messages |
rafalele
Unregistered
|
Posted Wednesday, January 12, 2005 @ 05:20:24
Hi and thanks in advance.
I want to format a column that has numeric data to show the next:
If number is 12345,5671, I want to see 12345,57. I´ve tried to use setPrecision and setScale methods of the colum, but it shows no change. Also if I ask the table for a value in this column I want it to give me the whole number 12345,5671.
|
Admin
Board Owner
Gender: Unspecified
Location:
Registered: Jul 2003
Status: Offline
Posts: 9
|
Posted Wednesday, January 12, 2005 @ 23:17:37
setPrecision and setScale methods are used to limit the size of the numeric value. Your case is formatting the text.
There are two ways to acheive this.
1) using column.setDocument() method. you need to create a Document object which can handle the formatting. Example have a look at IPDocument in the CustomerOrderDemo.
2)using DataMap class, format the text. refer column.setDataMapa(). Also have a look at CustomerOrderDemo for DataMap.
|
rafalele
Unregistered
|
Posted Thursday, January 13, 2005 @ 07:14:42
Thanks.
I cannot see the IPDocument implementation in the web example.
Using the DataMap the column shows the right formated number, but if I wan to get the whole number value using the getValueAt(int,int), it goes also through the convertDataToDisplay and I get the formated text.
This is my convertToDisplay code:
public Object convertDataToDisplay(Object val) {
try {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
return nf.format(val);
} catch(Exception e) {
return val;
}
}
What I need is that if I use getValueAt(int,int), it returns the val object.
|
Admin
Board Owner
Gender: Unspecified
Location:
Registered: Jul 2003
Status: Offline
Posts: 9
|
Posted Thursday, January 13, 2005 @ 17:20:36
For display purposes, you can use a small cell renderer.
class NumericCell extends JLabel implements CellComponent
{
Object val;
public void setValue(Object value)
{
val = value;
try {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
setText(nf.format(val));
} catch(Exception e) {
e.printStackTrace();
setText(val);
}
}
public Object getValue()
{
return val;
}
public JComponent getComponent()
{
return this;
}
}
yourColumn.setUserCellRenderer(new NUmericCell());
|
rafalele
Unregistered
|
Posted Friday, January 14, 2005 @ 06:05:27
Thanks, but it doesn´t work for me, I've done this:
public class NumericCell extends javax.swing.JLabel implements quick.dbtable.CellComponent {
int decimalDigits = -1;
Object val;
public void setValue(Object value) {
val = value;
if (decimalDigits == -1) {
setText(val.toString());
} else {
try {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(decimalDigits);
setText(nf.format(val));
} catch(Exception e) {
e.printStackTrace();
setText(val.toString());
}
}
}
public Object getValue() {
return val;
}
public JComponent getComponent() {
return this;
}
public void addActionListener(ActionListener listener) {
this.addActionListener(listener);
}
public void setPrecision(int precision) {
this.precision = precision;
}
}
And add the component to the column:
dbCol.setUserCellEditor(nc);
The problem is that I add the NumericCell but the code is not executed and the number is shown as it does not exists.
I use the dBTable.refreshDataObject(Vector, String[]) method to load data to the table before config the columns.
|
Admin
Board Owner
Gender: Unspecified
Location:
Registered: Jul 2003
Status: Offline
Posts: 9
|
Posted Friday, January 14, 2005 @ 10:13:35
You are trying to apply the celleditor to the column, but the cellcomponent extends from JLabel, so you will not be able to edit this column. If you are really trying to create celleditor, then extend from JTextField. Also, in getValue(), you need to parse the user modified text into number and return it, instead of returning the original value.
Ideally, you may want to set both cellRenederer & celleditor to this column. use the code I have given for cellrenderer and use your code with the above modification for celleditor.
|
|
|
|
|
|
|
|
Login |
|
In order to fully utilize the abilities of this board, you are required to register as a member. Registration is free, and allows you to do lots of things including turning on or off certain features of this board. Register now! |
|
|
Powered by CuteCast v2.0 BETA 2
Copyright © 2001-2003 ArtsCore Studios
|
|