user2499453
user2499453

Reputation: 31

How to disable column header sorting in a jtable after RowFilter filtering is applied

I have a JTable which is passed to another class when a JToggleButton is clicked, a popup with parameters for filtering appears and the JTable is filtered using RowFilter with the given parameters. When I display the filtering is happening as expected. But when I click on a column header sorting the rows get sorted based on the original JTable values, not only with the filtered ones.

The Image describing the issue.

How to disable such sorting? Please help me.

Upvotes: 3

Views: 1384

Answers (2)

camickr
camickr

Reputation: 324108

Sorting and filtering works fine for me without doing anything special.

I suggest you start by reading the section from the Swing tutorial on Sorting and Filtering.

So download the demo code and play with it. Make this code your starting code and then customize this code with your actual table data.

Upvotes: 1

aterai
aterai

Reputation: 9808

You might be able to override the isSortable(int) method of TableRowSorter to prevent that column from being sorted:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class DisableSortingTest {
  private static String[] columnNames = {"ID", "NAME", "SALARY"};
  private static Object[][] data = {
    {1, "abcd", 2000},
    {2, "xyz",  1800},
    {3, "ijkl", 4600},
    {4, "pqrs", 3400},
    {5, "efgh", 5000}
  };
  private final DefaultTableModel model = new DefaultTableModel(data, columnNames) {
    @Override public Class<?> getColumnClass(int column) {
      return getValueAt(0, column).getClass();
    }
  };
  private final JCheckBox checkBox = new JCheckBox("filter");
  private final RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() {
    @Override public boolean include(Entry<? extends TableModel, ? extends Integer> entry) {
      return "pqrs".equals(entry.getModel().getValueAt(entry.getIdentifier(), 1));
    }
  };
  private final JTable table = new JTable(model);
  private final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model) {
    @Override public boolean isSortable(int column) {
      return getRowFilter() == null;
    }
  };
  public JComponent makeUI() {
    table.setRowSorter(sorter);
    checkBox.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent e) {
        JCheckBox c = (JCheckBox) e.getSource();
        sorter.setRowFilter(c.isSelected() ? filter : null);
        sorter.setSortKeys(null);
      }
    });
    JPanel p = new JPanel(new BorderLayout());
    p.add(checkBox, BorderLayout.NORTH);
    p.add(new JScrollPane(table));
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new DisableSortingTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

Upvotes: 3

Related Questions