Trouble setting a column comparator to a JTable

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 09:25:23

问题


In another thread I found this comparator (bottom of post) for sorting JTable columns that could be composed of integers, strings, or both. I cannot figure out how to apply it to my JTable. My table was using the auto created row sorter before. I set that to false and I am now using:

TableRowSorter<MyTableModel> rowSorter = new TableRowSorter<MyTableModel>();
jtable.setRowSorter(rowSorter);
rowSorter.setComparator(0, c1);

I get an index out of bounds exception saying I am providing an invalid range. My table has multiple columns though. Is this the correct way to apply the comparator? I feel like this is not the way to do it.

Comparator c1 = new java.util.Comparator() {
    /**
     * Custom compare to sort numbers as numbers.
     * Strings as strings, with numbers ordered before strings.
     * 
     * @param o1
     * @param o2
     * @return
     */
@Override
            public int compare(Object oo1, Object oo2) {
                boolean isFirstNumeric, isSecondNumeric;
                String o1 = oo1.toString(), o2 = oo2.toString();


        isFirstNumeric = o1.matches("\\d+");
        isSecondNumeric = o2.matches("\\d+");

        if (isFirstNumeric) {
            if (isSecondNumeric) {
                return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
            } else {
                return -1; // numbers always smaller than letters
            }
        } else {
            if (isSecondNumeric) {
                return 1; // numbers always smaller than letters
            } else {
                isFirstNumeric = o1.split("[^0-9]")[0].matches("\\d+");
                isSecondNumeric = o2.split("[^0-9]")[0].matches("\\d+");

                if (isFirstNumeric) {
                    if (isSecondNumeric) {
                        int intCompare = Integer.valueOf(o1.split("[^0-9]")[0]).compareTo(Integer.valueOf(o2.split("[^0-9]")[0]));
                        if (intCompare == 0) {
                            return o1.compareToIgnoreCase(o2);
                        }
                        return intCompare;
                    } else {
                        return -1; // numbers always smaller than letters
                    }
                } else {
                    if (isSecondNumeric) {
                        return 1; // numbers always smaller than letters
                    } else {
                        return o1.compareToIgnoreCase(o2);
                    }
                }
            }
        }
    }
};

回答1:


when manually setting a RowSorter, you have to take care of keeping it in synch with the model yourself:

 TableRowSorter sorter = new TableRowSorter();
 table.setRowSorter(sorter);
 sorter.setModel(table.getModel());
 sorter.setComparator(myComparator);



回答2:


@kleopatra, you may not have a model when you get data from a raw text file for example, like a .csv. Therefore all columns are String while there are legit numbers in it, so you would want to sort those columns as numbers, not string (hence avoiding famous 1<11<10000<2<200...).

Thank you user1202394 for having found this other thread, could you provide us the link?

I managed to make it work as expected with three new code parts:

Comparator myComparator = new java.util.Comparator() {
    /**
     * Custom compare to sort numbers as numbers.
     * Strings as strings, with numbers ordered before strings.
     * 
     * @param o1
     * @param o2
     * @return
     */
    @Override
    public int compare(Object oo1, Object oo2) {
        boolean isFirstNumeric, isSecondNumeric;
        String o1 = oo1.toString(), o2 = oo2.toString();

        isFirstNumeric = o1.matches("\\d+");
        isSecondNumeric = o2.matches("\\d+");

        if (isFirstNumeric) {
            if (isSecondNumeric) {
                return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
            } else {
                return -1; // numbers always smaller than letters
            }
        } else {
            if (isSecondNumeric) {
                return 1; // numbers always smaller than letters
            } else {
                // Those lines throw ArrayIndexOutOfBoundsException
                //                        isFirstNumeric = o1.split("[^0-9]")[0].matches("\\d+");
                //                        isSecondNumeric = o2.split("[^0-9]")[0].matches("\\d+");

                // Trying to parse String to Integer.
                // If there is no Exception then Object is numeric, else it's not.
                try{
                    Integer.parseInt(o1);
                    isFirstNumeric = true;
                }catch(NumberFormatException e){
                    isFirstNumeric = false;
                }
                try{
                    Integer.parseInt(o2);
                    isSecondNumeric = true;
                }catch(NumberFormatException e){
                    isSecondNumeric = false;
                }

                if (isFirstNumeric) {
                    if (isSecondNumeric) {
                        int intCompare = Integer.valueOf(o1.split("[^0-9]")[0]).compareTo(Integer.valueOf(o2.split("[^0-9]")[0]));
                        if (intCompare == 0) {
                            return o1.compareToIgnoreCase(o2);
                        }
                        return intCompare;
                    } else {
                        return -1; // numbers always smaller than letters
                    }
                } else {
                    if (isSecondNumeric) {
                        return 1; // numbers always smaller than letters
                    } else {
                        return o1.compareToIgnoreCase(o2);
                    }
                }
            }
        }
    }
};

TableRowSorter sorter = new TableRowSorter();
table.setRowSorter(sorter);
sorter.setModel(table.getModel());
sorter.setComparator(myComparator);

// Apply Comparator to all columns
for(int i = 0 ; i < table.getColumnCount() ; i++)
    rowSorter.setComparator(i, c1);


来源:https://stackoverflow.com/questions/15162480/trouble-setting-a-column-comparator-to-a-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!