Pergunta

I am trying to sort integers in my 2nd column of my JTable. I used table.setAutoCreateRowSorter(true); but it is sorting by strings. I am trying to write some code to get around this but I cannot get it to work.

DefaultTableModel m = new DefaultTableModel();
String columnName = "Occurrences";  
m.addColumn("String");
m.addColumn(columnName);
JTable table = new JTable(m);
table.setAutoCreateRowSorter(true);


if (m.findColumn(columnName) == 2)
    {
            return Integer.class;
    } else {

            return null;
    }

This is the only way I can get this to even compile by adding Class<Integer> into my function. This doesn't work

Foi útil?

Solução

Your code isn't very clear, but if you want the second column class to be Integer, you just need to override it:

DefaultTableModel m = new DefaultTableModel() {
    @Override
    public Class<?> getColumnClass(int column) {
        if (column == 1) {
            return Integer.class;
        }
        return super.getColumnClass(column);
    }
};
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top