gpt4 book ai didi

java - 使用 Glazed Lists 和 JXTable 进行表外列排序

转载 作者:行者123 更新时间:2023-12-01 19:17:28 25 4
gpt4 key购买 nike

我正在使用 Glazed Lists 来排序和过滤 JXTable。

如何对表外值进行排序?也就是说,我希望能够以自己的方式格式化列值,但对原始值进行排序。

我当前的相关代码:

EventList<Foo> foos = GlazedLists.threadSafeList(new BasicEventList<Foo>());
foos.add(new Foo("bar", 5000000));

ObservableElementList.Connector<Foo> fooConnector = GlazedLists.beanConnector(Foo.class);
EventList<Foo> observedFoos = new ObservableElementList<Foo>(foos, fooConnector);

SortedList<Foo> sortedFoos = new SortedList<Foo>(observedFoos, null);

EventTableModel tableModel = new EventTableModel(sortedFoos, someTableFormat);
JXTable t = new JXTable(tableModel);

new TableComparatorChooser<Foo>(t, sortedFoos, false);

在此示例中,我希望将第二列中的值格式化为 5.0M 而不是 5000000,但如果我在列表中使用此值,则无法正确排序。

提前致谢。

最佳答案

也许您必须禁用 JXTable 排序,这样它就不会干扰 GL 排序?像这样的东西:

jxtable.setSortable(false);
jxtable.setAutoCreateRowSorter(false);
jxtable.setRowSorter(null);

...然后在 table 上安装 GlazedLists TableComparatorChooser,如下所示:

TableComparatorChooser.install(jxtable, sortedFoos, TableComparatorChooser.SINGLE_COLUMN);

或者你的意思是,你想在表中将 5000000 格式化为 5.0M,而不是在列表中?那么你只需要实现你的TableFormat的

public Object getColumnValue(E yourObject, int column)

返回 5000000 的 5.0M 表示形式。

...很可能是我没有完全理解问题并且这些答案没有帮助;-)

编辑:可运行示例...

查看 main 方法中的代码 - 特别是带有 START-END 注释的代码。我自己做了一个非常简单的例子,但是你应该明白我的意思。

哦...抱歉类/变量/...的命名;-)

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.TableComparatorChooser;
import java.util.Comparator;
import java.util.List;
import javax.swing.ComboBoxEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.jdesktop.swingx.JXTable;

public class Tester {

private static class MyObject implements Comparable<MyObject> {

private final int number;
private final String value;

public MyObject(int number, String value) {
this.number = number;
this.value = value;
}

public int getNumber() {
return number;
}

public String getValue() {
return value;
}

@Override
public int compareTo(MyObject t) {
return value.compareTo(t.getValue());
}
}

private static class MyTableFormat implements TableFormat<MyObject> {

@Override
public int getColumnCount() {
return 2;
}

@Override
public String getColumnName(int i) {
switch (i) {
case 0:
return "FormattedNumber";
case 1:
return "String";
default:
throw new IllegalStateException();
}
}

@Override
public Object getColumnValue(MyObject e, int i) {
switch (i) {
case 0:
return getNumberString(e.getNumber());
case 1:
return e.getValue();
default:
throw new IllegalStateException();
}
}

private Object getNumberString(int number) {
switch (number) {
case 1:
return "One";
case 2:
return "Two";
case 3:
return "Three";
default:
throw new IllegalStateException();
}
}
}

private static class MyComparator implements Comparator<MyObject> {

@Override
public int compare(MyObject t, MyObject t1) {
return Integer.valueOf(t.getNumber()).compareTo(Integer.valueOf(t1.getNumber()));
}

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
EventList<MyObject> list = new BasicEventList<MyObject>();
list.add(new MyObject(1, "A"));
list.add(new MyObject(2, "B"));
list.add(new MyObject(3, "C"));

SortedList<MyObject> sortedList = new SortedList<MyObject>(list);

EventTableModel<MyObject> tableModel = new EventTableModel<MyObject>(sortedList, new MyTableFormat());
JXTable jxtable = new JXTable(tableModel);

/**
* START
* - Deactivate JXTables build-in sorting
* - Install GlazedLists sorting
* - Set the comparator for the "string number" column
*/

// deactivate sorting of JXTable
jxtable.setSortable(false);
jxtable.setAutoCreateRowSorter(false);
jxtable.setRowSorter(null);

// enable GlazedLists sorting
TableComparatorChooser<MyObject> tcc = TableComparatorChooser.install(jxtable, sortedList, TableComparatorChooser.SINGLE_COLUMN);

// set the comparator for your "string number" column
List<Comparator> comparators = tcc.getComparatorsForColumn(0);
comparators.clear();
comparators.add(new MyComparator());

/**
* END
*/

JFrame f = new JFrame("Tester");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(jxtable));
f.pack();
f.setVisible(true);
}
}

关于java - 使用 Glazed Lists 和 JXTable 进行表外列排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5975973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com