0

i made a jtable , and everthing is working fine so fare , but still i got a problem in sorting the values of the table . some of my code:

    int nalt = (int) (1 + ((altmax - altmin) / incr));
    tabela = new JTable(new Object[nalt][6], colunas);
    for (int i = 0; i <= (nalt - 1); i = i) {
        for (double j = altmin; j <= altmax; j = j + incr) {
            Double tati = new Double(j);
            tabela.setValueAt(tati, i, 0);
            i = i + 1;
        }
    }
    tabela.setPreferredScrollableViewportSize(tabela.getPreferredSize());
    tabela.setFillsViewportHeight(true);
    tabela.setAutoCreateRowSorter(true);
    JScrollPane jps = new JScrollPane(tabela);
    panel2.add(jps);

the problem is , when i try to sort the columns from the higher to smaller number i'm getting something like this :

950
9000 
850
80000
7500
7
6000

if you could help me i would appreciate it

thanks

mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

2

The sort order shown appears to be based on String. As shown here, your (unseen) TableModel must return a suitable Comparable type for numeric sorting, e.g. Double.class. See the tutorial for examples.

Addendum: What am I doing wrong?

A minimal implementation of getColumnClass() for Double.class is shown here. Enable the automatic RowSorter and experiment to see the effect.

table.setAutoCreateRowSorter(true);
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • oh i see , but how i do that? i tried different ways , but i can't do that. I saw alot of examples , but they are a little different of my case , because i add the values to the table by table.setValueAt(,,,), and most of the cases that i saw they add the values by creating a array , and by editing their DefaultTableModel. Sorry for my ignorance , but i'm new here , just trying to learn . – user2627865 Aug 06 '13 at 13:12
  • Return `Double.class` for that column in your implementation of `getColumnClass()`. – trashgod Aug 06 '13 at 13:14
  • i did this : public Class table1(){ tabela.getColumnClass(0); return Double.class; } and this after setting the values of the table: table1(); tabela.setAutoCreateRowSorter(true); What i'm doing wrong? – user2627865 Aug 06 '13 at 16:58
  • A typical implementation of `getColumnClass()` for `Double.class` is shown [here](http://stackoverflow.com/a/10067560/230513). If this isn't helpful, please edit your question to include an [sscce](http://sscce.org/) that shows your current approach. It's hard to read code in a comment. – trashgod Aug 06 '13 at 17:20