在提交到绑定的Java DB之前,是否可以对jTable的内容进行排序/排序?
答案 0 :(得分:0)
您也可以在java代码中执行此操作,我假设您使用DefaultTableModel
作为JTable
的模型
首先从模型中获取数据,如下所示:
JTable grid = new JTable(); //your grid to be persisted
DefaultTableModel model = grid.getModel(); //get the model (DefaultTableModel)
Vector vectorData = model.getDataVector(); //the data (Vector of vectors)
然后使用sort()
传递java.util.Collections
的{{1}}方法来实现:
Comparator
这是我要从model.getDataVector();
检索的模拟数据 public static final Comparator<Vector> COMPARATOR_BY_KEY = new Comparator<Vector>() {
public int compare(Vector o1, Vector o2) {
//Get the Column to be used as criteria for sorting
//Need to be casted (cast to your real data type) as this is a Vector of Vectors
//In this example I'm assuming that cell data type is `String`
String elementAtCell1 = (String) o1.elementAt(0);
String elementAtCell2 = (String) o2.elementAt(0);
return elementAtCell1.compareTo(elementAtCell2);
}
};
因此,在将数据发送到持久层之前,请将其称为:
Vector vector = new Vector() {
{
add(new Vector() {
{
add("KEYA");
add(100);
add(1.1);
}
});
add(new Vector() {
{
add("KEYC");
add(11);
add(2.2);
}
});
}
};