craftleft.gif (3002 bytes)QuickTable
Home | API | Tutorial | Download | Support | Contact topblack.gif (108 bytes)


QuickTable User Cp  |  Register  |  Members  |  Search  |  Help
    |- QuickTable Discussion Forums > Column Post New Topic   Post A Reply
comparator example printer friendly version
next newest post | next oldest post
Author Messages
bwrwzw
Private

Gender: Unspecified
Location:
Registered: Sep 2007
Status: Offline
Posts: 2

Click here to see the profile for bwrwzw Send email to bwrwzw Send private message to bwrwzw Find more posts by bwrwzw Edit or delete this message Reply w/Quote
Posted Wednesday, September 12, 2007 @ 22:19:44  

Do you have another example, besides Norwegian, that shows how to use the Comparator class? The problem I'm having is that my data is not sorting signed decimal data correctly. This is how my data is being sorted:

-1.823
-2.431
-23.516
-4.487
-9.484
0.000
1.250
10.012
105.915
11.231
13.044
etc.

To give you more information: The data is read in using an Object[][]. I have used new DecimalFormat("0.000") to format the data prior to storing in the object array. Apparently the quickTable is sorting the data as strings instead of floats as desired. Here is the code used to tell the table that this data in column 4 is numeric:

Column c = dBTable1.getColumn(4);
c.setReadOnly(true);
c.setType(Types.NUMERIC);
c.setSigned(true);
c.setPrecision(8);
c.setScale(3);

Is there something else I need to be doing to let the quickTable know to treat this data as numeric and sort accordingly?

[Edit by bwrwzw on Thursday, September 13, 2007 @ 13:00:33]

Admin
Board Owner

Gender: Unspecified
Location:
Registered: Jul 2003
Status: Offline
Posts: 9

Click here to see the profile for Admin Send email to Admin Send private message to Admin Find more posts by Admin Edit or delete this message Reply w/Quote
Posted Friday, September 14, 2007 @ 02:53:36  

Since the data in the object array is not Decimal, quicktable is using string comparison to do the sorting. Instead, create the array using the original object and apply the formatting using DataMap class

Read api
http://quicktable.org/doc/quick/dbtable/DataMap.html

bwrwzw
Private

Gender: Unspecified
Location:
Registered: Sep 2007
Status: Offline
Posts: 2

Click here to see the profile for bwrwzw Send email to bwrwzw Send private message to bwrwzw Find more posts by bwrwzw Edit or delete this message Reply w/Quote
Posted Friday, September 14, 2007 @ 10:00:02  

This helped some. When I store the data as Double objects then use the QuickTable, the sorting works great. However, when I try to apply formatting so that it only shows 3 decimal places, the sorting reverts back to String sorting. Here is the code used to do the DataMap:

c.setDataMap(new DataMap() {
DecimalFormat df = new java.text.DecimalFormat("0.000");
public Object convertDataToDisplay(Object object) {
return df.format((Double)object);
}
public Object convertDataToStore(Object object) {
try {
return new Double(df.parse((String)object).doubleValue());
} catch(Exception e) {
return new Double("0");
}
}
});

Your help is appreciated!

Admin
Board Owner

Gender: Unspecified
Location:
Registered: Jul 2003
Status: Offline
Posts: 9

Click here to see the profile for Admin Send email to Admin Send private message to Admin Find more posts by Admin Edit or delete this message Reply w/Quote
Posted Sunday, September 16, 2007 @ 20:58:31  

I think I gave the wrong solution. DataMap is not appropriate for this situation, since it converts the data to string, which does not help in sorting. We have to have the underlying data in Double and just the display need to be modified to the display format.

Probably this feature should be added to QuickTable.

In the mean time, one solution is to create your own cellcomponent (cellrenderer & editor) which can convert the underlying Double data into formated string. You can use the following approach, please make sure the underlying data (Object array or resultset) is Double.

Code:

class DoubleRenderer extends JLabel implements CellComponent
{
DecimalFormat df = new java.text.DecimalFormat("0.000");

public void setValue(Object value)
{
setText(df.format((Double)value));
}
public JComponent getComponent()
{
return this;
}

//for rendererer the below methods will not be called
public Object getValue()
{
returen null;
}
public void addActionListener(ActionListener listener){}
}

class DoubleEditor extends JTextField implements CellComponent
{
DecimalFormat df = new java.text.DecimalFormat("0.000");

public void setValue(Object value)
{
setText(df.format((Double)value));
}
public JComponent getComponent()
{
return this;
}
public Object getValue()
{
return new Double(df.parse(getText()).doubleValue());
}
public void addActionListener(ActionListener listener){}
}

//if readonly table, you just need renderer
yourColumn.setUserCellRenderer(new DoubleRenderer())
yourColumn.setUserCellEditor(new DoubleEditor()),

You don't need to create any comparator. Since underlying data is Double it should sort properly.

Post New Topic   Post A Reply Jump to:
Contact Us | QuickTable - A Java DBGrid | Privacy Policy All times are GMT -5 Hours.
Welcome to QuickTable Forums, Guest!  
Login
Username :
Password :
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