1
\$\begingroup\$

The purpose of this custom TableRowSorter is to provide me with a different sort order of the columns. It changes the sort order from
SortOrder.UNSORTED => SortOrder.ASCENDING => SortOrder.DESCENDING => SortOrder.ASCENDING => SortOrder.DESCENDING => ...
to
SortOrder.UNSORTED => SortOrder.ASCENDING => SortOrder.DESCENDING =>SortOrder.UNSORTED => SortOrder.ASCENDING => SortOrder.DESCENDING => ...

The program has variables in to continue the sort order automatically even if there is an initial sort key in the column it is about to sort like there is in the example implementation below.

I would like to know if you think the program can do what it is meant to do properly. I would particularly be interested in your thoughts on the efficiency of the program and if any improvements can be made to it.

ADU_SortOrder.java

package order;

import java.util.ArrayList;
import java.util.List;

import javax.swing.SortOrder;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class ADU_SortOrder<M extends TableModel> extends TableRowSorter<M> {
    public ADU_SortOrder(M model) {
        setModel(model);
    }

    boolean firstTime = true; //Needed in case there are any initial sort keys
    int columnHolder = -1;

    @Override
    public void toggleSortOrder(int column) {
        List<? extends SortKey> sortKeys = getSortKeys();
        if(sortKeys.size() == 0) { //For if there are no initial sort keys
            List<SortKey> keys = new ArrayList<SortKey>();
            keys.add(new SortKey(column, SortOrder.ASCENDING));
            setSortKeys(keys);
            return;
        }

        if (sortKeys.size() > 0 && columnHolder == column || firstTime) {
            if(firstTime) {
                firstTime = false;
                columnHolder = column;
                if(column != sortKeys.get(0).getColumn()) {
                    List<SortKey> keys = new ArrayList<SortKey>(getSortKeys());
                    keys.set(0, new SortKey(column, SortOrder.ASCENDING));
                    setSortKeys(keys);
                    return;
                }
            }

            List<SortKey> keys = new ArrayList<SortKey>(getSortKeys());
            keys.set(0, new SortKey(column, setNextOrder(sortKeys.get(0).getSortOrder())));
            setSortKeys(keys);
            return;
        } else if(sortKeys.size() > 0 && columnHolder != column && !firstTime) {
            List<SortKey> keys = new ArrayList<SortKey>(getSortKeys());
            keys.set(0, new SortKey(column, SortOrder.ASCENDING));
            setSortKeys(keys);
            columnHolder = column;
            return;
        }
        super.toggleSortOrder(column);
    }

    private SortOrder setNextOrder(SortOrder order) {
        switch (order) {
            case ASCENDING:
                return SortOrder.DESCENDING;
            case DESCENDING:
                return SortOrder.UNSORTED;
            case UNSORTED:
                return SortOrder.ASCENDING;
            default:
                return SortOrder.UNSORTED;
        }
    }
}

JTableTest.java
Example Implementation

import java.awt.GridLayout;

import java.util.List;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import javax.swing.RowSorter;
import javax.swing.SortOrder;

import order.ADU_SortOrder;

@SuppressWarnings("serial")
public class JTableTest extends JFrame {
    private JTableTest() {
        super("JTable Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(1, 1));
        createPanel();
        pack();
        setVisible(true);
    }

    JPanel panel = new JPanel(new GridLayout(1, 1));
    JScrollPane scroll;

    private void createPanel() {
        Object[] headers = {"Select", "Title", "Artist", "Length", "Title"};
        Object[][] sampleData = {{true, "Bat Outta Hell", "Meat Loaf", "673", "Bat Outta Hell"},
                {false, "Spanish Train", "Chris De Burgh", "358", "Spanish Train"},
                {true, "Bat Outta Hell", "Meat Loaf", "673", "Bat Outta Hell"}};
        JTable table = new JTable(sampleData, headers);

        //Sort Order Part
        ADU_SortOrder<TableModel> sortOrder = new ADU_SortOrder<TableModel>(table.getModel());
        List<RowSorter.SortKey> sortKeys = new ArrayList<>();
        sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
        sortOrder.setSortKeys(sortKeys);
        table.setRowSorter(sortOrder);

        scroll = new JScrollPane(table);
        panel.add(scroll);
        getContentPane().add(panel);
    }

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

Any other comments, tips or suggestions are welcomed and appreciated.

\$\endgroup\$
1
\$\begingroup\$
        if(sortKeys.size() == 0) { //For if there are no initial sort keys

It's more canonical to write

        if (sortKeys.isEmpty()) {

Also, either the size is 0 or greater than 0. There are no other alternatives. So you don't need to check if the size is greater than 0. If you did not return from this block, the size must be greater than 0. So you don't need to check.

        if (sortKeys.size() > 0 && columnHolder == column || firstTime) {

and

        } else if(sortKeys.size() > 0 && columnHolder != column && !firstTime) {

If we drop the size check, these could just be

        if (columnHolder == column || firstTime) {

and

        } else {

This

        super.toggleSortOrder(column);

is unnecessary. You always return before reaching this.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer. In your opinion is it a useful piece of code to have or would the majority of JTables not require it? \$\endgroup\$ – Dan Nov 26 '16 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.