I want to sort a column with int in it in a JTable. I use TableRowSorter
with the sort order SortOrder.ASCENDING
. But somehow it sorts not how I actually want it.
What I want:
What I get:
What is the Problem? And how can I fix it?
Here is the SSCCE:
ResultGUI.java
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
public class ResultGUI {
protected static JTable ranglisteTable = new JTable();
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 368, 316);
frame.setLayout(null);
//MODEL
Vector<Object> data = new Vector<Object>();
Vector<Object> row = new Vector<Object>();
row.addElement(3);
data.addElement(row);
Vector<Object> row2 = new Vector<Object>();
row2.addElement(2);
data.addElement(row2);
Vector<Object> row3 = new Vector<Object>();
row3.addElement(1);
data.addElement(row3);
Vector<Object> row4 = new Vector<Object>();
row4.addElement(22);
data.addElement(row4);
Vector<Object> row5 = new Vector<Object>();
row5.addElement(13);
data.addElement(row5);
Vector<String> columnNames = new Vector<String>();
columnNames.addElement("Points");
TableModel model = new DefaultTableModel(data, columnNames);
for(int i = 0; i < 10; i++){
ranglisteTable.setRowHeight(i, 30);
}
ranglisteTable.setBounds(109, 0, 128, 277);
ranglisteTable.setModel(model);
//SORTING
TableRowSorter<TableModel> sorter = new TableRowSorter<>(ranglisteTable.getModel());
ranglisteTable.setRowSorter(sorter);
List<RowSorter.SortKey> sortKeys = new ArrayList<>();
sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
sorter.setSortKeys(sortKeys);
sorter.sort();
frame.add(ranglisteTable);
frame.setVisible(true);
}
}
you try this code
import java.util.ArrayList;
import java.util.Date;
import javax.swing.DefaultRowSorter;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
public class ResultGUI extends JFrame
{
public ResultGUI()
{
String[] columnNames = {"points"};
Object[][] data =
{{ new Long(1)}, { new Long(2)},{ new Long(9) },{ new Long(4)}
};
final JTable table = new JTable(data, columnNames)
{
public Class getColumnClass(int column)
{
// Lookup first non-null data on column
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
return o.getClass();
}
return Object.class;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setAutoCreateRowSorter(true);
// DefaultRowSorter has the sort() method
DefaultRowSorter sorter = ((DefaultRowSorter)table.getRowSorter());
ArrayList list = new ArrayList();
list.add( new RowSorter.SortKey(0, SortOrder.ASCENDING) );
sorter.setSortKeys(list);
sorter.sort();
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
}
public static void main(String[] args)
{
ResultGUI frame = new ResultGUI();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}