Is there a way to sort a JTable programmatically?
I have my JTable's sort working (with setRowSorter) so that when the user presses any of the columns, the table gets sorted.
I know, SWingX JXTable would probably work, but I'd rather not go through the hassle because everything else is pretty much working now and I don't know how well NetBeans' visual editor handles JXTable etc.
EDIT: The selected answer is referring to my (now removed) statement that the answer from Sun's pages didn't work for me. That was just an environment issue caused by my ignorance.
We can sort a JTable in a particular column by using the method setAutoCreateRowSorter() and set to true of JTable class.
TableRowSorter uses Comparator s for doing comparisons. The following defines how a Comparator is chosen for a column: If a Comparator has been specified for the column by the setComparator method, use it. If the column class as returned by getColumnClass is String , use the Comparator returned by Collator.
Class DefaultTableModel. This is an implementation of TableModel that uses a Vector of Vectors to store the cell value objects. Warning: DefaultTableModel returns a column class of Object .
Works fine for me:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableBasic extends JPanel
{
public TableBasic()
{
String[] columnNames = {"Date", "String", "Integer", "Boolean"};
Object[][] data =
{
{new Date(), "A", Integer.valueOf(1), Boolean.TRUE },
{new Date(), "B", Integer.valueOf(2), Boolean.FALSE},
{new Date(), "C", Integer.valueOf(19), Boolean.TRUE },
{new Date(), "D", Integer.valueOf(4), Boolean.FALSE}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
// Returning the Class of each column will allow different
// renderers and editors to be used based on Class
public Class getColumnClass(int column)
{
switch (column)
{
case 0: return Date.class;
case 2: return Integer.class;
case 3: return Boolean.class;
}
return super.getColumnClass(column);
}
};
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setAutoCreateRowSorter(true);
// DefaultRowSorter has the sort() method
ArrayList<RowSorter.SortKey> list = new ArrayList<>();
DefaultRowSorter sorter = ((DefaultRowSorter)table.getRowSorter());
sorter.setSortsOnUpdates(true);
list.add( new RowSorter.SortKey(2, SortOrder.ASCENDING) );
sorter.setSortKeys(list);
sorter.sort();
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Table Basic");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TableBasic());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
SwingUtilities.invokeLater( () -> createAndShowGUI() );
}
}
Next time post your minimal, reproducible example when something doesn't work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With