SOURCECODE |
How to... create a sorted Table
Description:
Sorted tables are like normal ones, except that they have a certain order for displaying the content.
This example is almost the same as in "How to create a table". Only the comparator is new, which is necessary to define the sort order for the table.
To do so, you have to implement the method public int compare(Object o1, Object o2)
, which shall return an int value corresponding to your desired sort order. As described in the Java API, the int should be negative if the second Object is of a higher value, zero if the Objects are "equal" and of positive if the first Object shall be on top. For more information on the Interface java.uitl.Comparator, please refer to the latest API on Java.sun.com.
In this case, we compare the bid() value of the VideoCassettes in order to display them by their price, cheap ones first.
ToDo's:
- Initialize the Gate where the FormSheet shall be displayed in
- Get the Catalog the content shall be displayed of
- Create the Comparator and implement the method
public int compare(Object o1, Object o2)
to return an integer suiteable to the order of the compared elements
- Call
SingleTableFormSheet.create(String caption, Catalog c, Gate g, Compatator co, TableEntryDescriptor ted)
to initialize the SingleTableFormSheet AND assign it to the UIGate
Uses:
FormSheet SaleProcess SingleTableFormSheet AbstractTableEntryDescriptor AbstractTableModel
JAbstractTable Catalog CatalogTableModel DefaultCatalogItemTED
import sale.*;
import data.stdforms.*;
import util.swing.*;
import data.*;
import data.swing.*;
import java.util.*;
public class SeeVideoSorted extends SaleProcess {
protected UIGate viewGate;
public SeeVideoSorted() {
super ("SeeVideoProcess");
}
public void setupMachine() {
1
//The UIGate where the table shall be displayed in
viewGate = new UIGate(null, null);
2
//The Catalog that is to be displayed (the VideoCatalog of the Tutorial)
Catalog c = Shop.getTheShop().getCatalog("Video-Catalog");
3
//the implementation of the used comparator
//surely not a good one, but suitable for our demonstration
Comparator myComparator = new Comparator() {
//the method used for the comparison
public int compare(Object o1, Object o2) {
//resolve the first cassette
VideoCassette vc1 = (VideoCassette)o1;
NumberValue bid1 = (NumberValue)((QuoteValue)vc1.getValue()).getBid();
//the second one
VideoCassette vc2 = (VideoCassette)o2;
NumberValue bid2 = (NumberValue)((QuoteValue)vc2.getValue()).getBid();
//the bidīs as int
int i1 = bid1.getValue().intValue();
int i2 = bid2.getValue().intValue();
return i1 - i2;
}
};
4
//The SingleTableFormSheet, which displays the Catalog in the order defined above in the viewGate
SingleTableFormSheet stfs = SingleTableFormSheet.create("VideoCatalog",
c,
viewGate,
myComparator,
new DefaultCatalogItemTED());
}
public Gate getInitialGate() {
setupMachine();
return viewGate;
}
}