I have a JTable where I have set table.setAutoCreateRowSorter(true);. My main objective is to have one row that is always at the top, regardless of any column sort order. First thing I did was modify the compareTo method for my cell datatype so that it would understand a flag that means “less than anything else” and therefore sort that item always to the top. Unfortunately, that made it so that the given row would always be at the very top or the very bottom, depending on the sort order for the column. So when the sort order is descending, what I need is to set the cell so that its effective value is greater than anything else. Only problem is I can’t figure out how to get the column sort order.
On this page, one suggested answer is along the lines of
if (myTable.getRowSorter().getSortKeys().get(column).getSortOrder == SortOrder.ASCENDING) {
...
}
Unfortunately, getSortKeys() always returns an empty list. So I’m back at square one. I also considered adding a RowSorterListener, but this appears to be a listener for when the sorter is changed, not when the sort order is changed.
Any help? Thanks!
EDIT: Here is code that demonstrates part of my problem. Note the System.out.println, where the result is always zero. I would like to get the row sort order for the columns, but none seems to exist.
package tablecolumn;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.table.AbstractTableModel;
public class Tablecolumn extends JFrame {
JScrollPane scroll;
JTable table;
MyTableModel model;
static String[] columnnames = new String[] {"A", "B", "C"};
public class MyTableModel extends AbstractTableModel {
public List<List<String>> entries;
MyTableModel() {
entries = new ArrayList<List<String>>();
for (int i=0; i<5; i++) {
List<String> cols = new ArrayList<String>();
cols.add("X" + i);
cols.add("Y" + i);
cols.add("Z" + i);
entries.add(cols);
}
}
@Override
public int getRowCount() {
return entries.size();
}
@Override
public int getColumnCount() {
return columnnames.length;
}
@Override
public Object getValueAt(int r, int c) {
List<? extends RowSorter.SortKey> rsk = table.getRowSorter().getSortKeys();
System.out.println(rsk.size());
return entries.get(r).get(c);
}
@Override
public Class getColumnClass(int c) {
return String.class;
}
@Override
public String getColumnName(int c) { return columnnames[c]; }
@Override
public boolean isCellEditable(int row, int col) { return false; }
}
public Tablecolumn() {
scroll = new JScrollPane();
model = new MyTableModel();
table = new JTable();
table.setModel(model);
table.setAutoCreateRowSorter(true);
table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
table.getTableHeader().setReorderingAllowed(false);
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
scroll.setViewportView(table);
scroll.setMinimumSize(new Dimension(200, 200));
scroll.setPreferredSize(new Dimension(800, 600));
GroupLayout layout2 = new GroupLayout(this.getContentPane());
this.setLayout(layout2);
layout2.setVerticalGroup(
layout2.createSequentialGroup()
.addComponent(scroll)
);
layout2.setHorizontalGroup(
layout2.createParallelGroup()
.addComponent(scroll)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Tablecolumn().setVisible(true);
}
});
}
}
I think the problem is that if you use
table.setAutoCreateRowSorter(true);, there’s a bug that makesmyTable.getRowSorter().getSortKeys()always return an empty list. On the other hand, if you add a row sorter of your own, then it works.I needed to add this code:
Now,
getSortKeys()will return a list of columns and their sort orders. That list is variable length, depending on which columns you’ve clicked, so you’ll have to iterate through the list looking atmyTable.getRowSorter().getSortKeys().get(index).getColumn()andmyTable.getRowSorter().getSortKeys().get(index).getSortOrder(), where the latter contains values ofSortOrder.ASCENDINGorSortOrder.DESCENDING.