简体   繁体   English

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

[英]Out-of-table column sorting with Glazed Lists and a JXTable

I'm using Glazed Lists to sort and filter a JXTable. 我正在使用Glazed Lists排序和过滤JXTable。

How can I sort on out-of-table values? 如何对表外值排序? That is, I would like to be able to format column values in my own way, yet sort on raw values. 也就是说,我希望能够以自己的方式设置列值的格式,而对原始值进行排序。

My current relevant code: 我当前的相关代码:

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);

In this example, I would like to format the value in the second column as 5.0M rather than 5000000 , but if I use this value in the list, it won't sort properly. 在此示例中,我想将第二列中的值格式化为5.0M而不是5000000 ,但是如果我在列表中使用此值,它将无法正确排序。

Thanks in advance. 提前致谢。

Maybe you have to disable the JXTable Sorting, so it does not interfere with the GL sorting? 也许您必须禁用JXTable排序,以便它不干扰GL排序? Something like: 就像是:

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

... and then install GlazedLists TableComparatorChooser on the table like: ...然后在表上安装GlazedLists TableComparatorChooser,如下所示:

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

Or do you mean, you want to format 5000000 as 5.0M in the table, not in the List? 还是您要在表中而不是在列表中将5000000格式化为5.0M? Then you would only have to implement your TableFormat's 然后,您只需实现TableFormat的

public Object getColumnValue(E yourObject, int column)

to return the 5.0M representation of 5000000. 返回5000000的5.0M表示形式。

... could well be, that I did not fully understand the problem and these answers are not helping ;-) ...很可能是我没有完全理解问题,这些答案也无济于事;-)

EDIT: Runnable example... 编辑:可运行的示例...

Look at the code in the main method - especially the code with the START-END comment. 查看main方法中的代码-尤其是带有START-END注释的代码。 I made my own very simple example, but you should understand, what I mean. 我做了一个非常简单的例子,但您应该理解我的意思。

Oh... sorry for the naming of classes/variables/... ;-) 哦...很抱歉为类/变量/ ...的命名;-)

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM