在JTable中按列对行进行排序 [英] Sorting rows by columns in JTable

查看:719
本文介绍了在JTable中按列对行进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您单击JTable上的列标题时,它会按字母顺序对行进行排序。除了一个列之外,这适用于我的所有列。在此列中,值都是字符串,但包含数字。我需要根据它们的十进制值而不是字符串值对它们进行排序。

When you click the column header on my JTable, its sorts the rows alphabetically. This works for all of my columns except for one. In this column the values are all Strings, but contain numbers. I need them to be sorted based off of their decimal value and not string value.

任何想法如何实现这一点?

Any idea how to accomplish this?

推荐答案

请阅读有关 JTable <的教程/ a>包含TableRowSorter示例,

please read tutorial about JTable that's contains TableRowSorter example,

您的答案是这些代码行,正确设置列类

your answer is these codes lines, set 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;
                }
            } 

这篇关于在JTable中按列对行进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆