我有一个JXtable。我想阻止排名前三行。基本上,前三行应始终位于顶部,其余的应根据其值进行排序。
在SO上有一个类似的问题,但我不确定如何真正应用于我的使用 Sort ROW except last row
主要区别在于我正在使用JXTable。有没有一种简单的方法可以做到这一点,我错过了?
这是我的JXtable代码
jTable1 = new JXTable();
if (isClickable) {
jTable1.getTableHeader().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
//set the sorter here
} else {
jTable1.setSortable(false);
}
答案 0 :(得分:1)
嗯,我花了一点时间尽力完善这一点,但这是我的工作范例。
基本上,我创建了一个名为ComparableWrapper
的包装类,它实现了Comparable,因此可以操作每列的排序。对于表模型,我添加了setData(Object[][] data)
方法,该方法自动包装ComparableWrapper
中的所有数据值。 sorted
构造函数中的ComparableWrapper
参数确定值是否应该按正常方式排序,或者无论如何都保持在顶部。
您必须在几个地方手动更改列的类类型。无论如何,这是我的SSCCE:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;
import org.jdesktop.swingx.JXTable;
public class TableTest extends JFrame {
public TableTest() {
super("JXTable - first 3 rows aren't sorted");
setLayout(new BorderLayout());
MyTableModel model = new MyTableModel();
JXTable table = new JXTable();
table.setModel(model);
Object[][] initialData = {
{"Zoe", new Integer(26)},
{"Adam", new Integer(29)},
{"Trisha", new Integer(33)},
{"Reed", new Integer(20)},
{"John", new Integer(3)},
{"Kyle", new Integer(102)},
{"Billy Bob", new Integer(27)},
{"Sandra", new Integer(87)},
{"Steve", new Integer(50)},
{"Guy", new Integer(23)},
{"Kenzie", new Integer(25)},
{"Mary", new Integer(27)},
{"Sally", new Integer(12)},
{"Joe", new Integer(101)},
{"Billy", new Integer(44)},
{"Bob", new Integer(83)},
};
model.setData(initialData);
JScrollPane tableScroll = new JScrollPane(table);
add(tableScroll, BorderLayout.CENTER);
table.setFillsViewportHeight(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 600);
setVisible(true);
setLocationRelativeTo(null);
}
private class MyTableModel extends AbstractTableModel {
String[] columns = { "First Name", "Age" };
Object[][] data = {};
public void setData(Object[][] data) {
for (int i=0; i<data.length; i++) {
// Don't sort top 3 rows
boolean sorted = i < 3 ? false : true;
// Wrap each object so the sorting can be manipulated.
// Change these class types as needed.
data[i][0] = new ComparableWrapper<String>((String) data[i][0], sorted);
data[i][1] = new ComparableWrapper<Integer>((Integer) data[i][1], sorted);
}
this.data = data;
fireTableDataChanged();
}
@Override
public Class<?> getColumnClass(int col) {
return ComparableWrapper.class;
}
@Override
public int getColumnCount() {
return columns.length;
}
@Override
public int getRowCount() {
return data.length;
}
@Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
@Override
public String getColumnName(int col) {
return columns[col];
}
}
private class ComparableWrapper<T> implements Comparable<ComparableWrapper<T>> {
private Comparable<T> object;
private boolean sorted;
public ComparableWrapper(Comparable<T> object, boolean sorted) {
this.object = object;
this.sorted = sorted;
}
@Override
public int compareTo(ComparableWrapper<T> o) {
if (object instanceof Comparable<?>) {
if (sorted && o.isSorted()) {
// Sort normally by default
return object.compareTo(o.getObject());
} else {
// If an un-sorted row is being compared, don't compare at all
return 0;
}
}
// Fallback
return 0;
}
public T getObject() {
return (T) object;
}
public boolean isSorted() {
return sorted;
}
@Override
public String toString() {
return object.toString();
}
}
public static void main(String[] args) {
new TableTest();
}
}