Question

Java SE 6 Arrows for ASC/DESC are above Header-Text (is must have by customer)

I use to show arrows above header-text:

TableRowSorter sorter = new TableRowSorter();
sorter.setModel(table.getModel());
table.setRowSorter(sorter);

or

table.setAutoCreateRowSorter(true);

Arrows for ASC/DESC are above Header-Text, that's right. I want to use my own comparator, but in my example, comparator is called, but it does not functioned. What code is to change to sort table-rows by my own comparator with arrows above header-text sorting asc/desc?

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import javax.swing.SwingUtilities; 
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class JTableExample extends JFrame {

    private JTable table;

    private FooTableModel model;

    public JTableExample() {
        super("JTableExample");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
            String plaf = UIManager.getSystemLookAndFeelClassName();
            try {
                UIManager.setLookAndFeel(plaf);
            } 
            catch (ClassNotFoundException e) {} 
            catch (InstantiationException e) {} 
            catch (IllegalAccessException e) {} 
            catch (UnsupportedLookAndFeelException e) {}
            SwingUtilities.updateComponentTreeUI(this);

        Object[][] rowData = { { "a", "c" }, { "b", "b" }, { "c", "a" } };
        Object[] columnHeaders = { "Header1", "Header2" };
        model = new FooTableModel(rowData, columnHeaders);

        table = new JTable(model);
        table.setAutoCreateRowSorter(true);

        table.getTableHeader().addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
               model.sortByColumn(table.columnAtPoint(evt.getPoint()));
            }
        });

        Container c = getContentPane();
        c.add(new JScrollPane(table), BorderLayout.CENTER);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new JTableExample();
    }

    class FooTableModel extends DefaultTableModel {
        public FooTableModel(Object[][] rowData, Object[] headers) {
            super(rowData, headers);
        }

        public void sortByColumn(final int clm) {
            Collections.sort(this.dataVector, new Comparator() {
                public int compare(Object o1, Object o2) {
                    Vector v1 = (Vector) o1;
                    Vector v2 = (Vector) o2;


                    String s1 = (String) v1.get(clm);
                    String s2 = (String) v2.get(clm);

                    return s1.compareTo(s2);
                }
            });
        }
    }
}

No correct solution

OTHER TIPS

Whatever your problem is exactly (hard to tell from your description), you are doing the sorting incorrectly: in Swing JTable the sorting is considered the responsibility of the view (vs. the model as in your implementation).

Custom comparators (per column) are configured in the rowSorter, something like:

table.setAutoCreateRowSorter(true);
((TableRowSorter) table.getRowSorter()).setComparator(columnIndex, myComparator);

BTW: the location of the sort icon is controlled by the LAF, not much you can do to change that (short of writing a custom LAF)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top