Stackie Overflower
Stackie Overflower

Reputation: 211

JTable sorting int in undesired order

I'm trying to create a JTable combined with DefaultTableModel. My problem is that, when I'm clicking to sort it (row with prices) it sorts incorrectly.

Say we have row:

2
6
3
112
42
1

What I want:

1
2
3
6
42
112

What I get:

1
112
2
3
42
6

Any suggestions or solutions?

Upvotes: 1

Views: 136

Answers (1)

EricR
EricR

Reputation: 1497

An example from my one of my project's codebase. This belongs in your TableModel

    @Override
    public Class<?> getColumnClass(int col) {
            switch (col) {
                case 0:
                    return Integer.class;
                case 1:
                    return String.class;
                case 2:
                    return String.class;
                case 3:
                    return Integer.class;
            }
    }

Upvotes: 7

Related Questions