2

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?

2
  • By "contain numbers" do you mean a string like "50 apples" or just a number like "50" with the type of string? Commented Jun 3, 2011 at 21:07
  • Both answers correctly refer to getColumnClass(). See also Class Literals as Runtime-Type Tokens. Commented Jun 4, 2011 at 0:20

2 Answers 2

11

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;
                }
            } 
Sign up to request clarification or add additional context in comments.

1 Comment

+1, the proper solution is to store the numbers as Double objects and not String objects. Not only will they be sorted properly but the will be rendered proprely as well with right justification.
4

The default row sorter will sort based on the column class. If the column class is Object (the default) then it uses the toString() method. If you can change what you put into the column to something that implements the Comparable interface (e.g. Integer/Double) then it will use that comparator instead. You will have to also change the column class on the table model.

In order to do that you will have to extend DefaultTableModel (or implement AbstractTableModel or TableModel) and override the getColumnClass() method.

If you can't change the data that goes into the column (for some reason you want to store strings there) then you will have to modify the RowSorter for the table.

DefaultRowSorter rowSorter = new DefaultRowSorter();
rowSorter.setComparator(numberColumnIndex,numberSortingComparator);
table.setRowSorter(rowSorter);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.