Java – sort rows by column in JTable

When you click a column heading on JTable, it sorts the rows alphabetically This applies to all my columns except one In this column, the values are strings, but contain numbers I need to sort them according to their decimal values instead of string values

Know how to do this?

Solution

Please read the tutorial on JTable, which includes the tablerowsorter example,

Your answer is these lines of code, set the column class correctly

public Class getColumnClass(int c) {
   return getValueAt(0,c).getClass();
}

// or Could be in most cases hardcoded,and I'm using that too

            @Override
            public Class<?> getColumnClass(int colNum) {
                switch (colNum) {
                    case 0:
                        return Integer.class;
                    case 1:
                        return Double.class;
                    case 2:
                        return Long.class;
                    case 3:
                        return Boolean.class;
                    case 4:
                        return String.class;
                    case 5:
                        return Icon.class;
                    /*case 6:
                    return Double.class;
                    case 7:
                    return Double.class;
                    case 8:
                    return Double.class;*/
                    default:
                        return String.class;
                }
            }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>