简体   繁体   English

如何在应用RowFilter过滤后禁用jtable中的列标题排序

[英]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. 我有一个JTable ,当单击JToggleButton时会传递给另一个类,出现带有过滤参数的弹出窗口,并使用带有给定参数的RowFilter过滤JTable 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. 但是,当我单击列标题排序时,行将根据原始JTable值进行排序,而不仅仅是过滤后的行。

描述问题的图像。

How to disable such sorting? 如何禁用这样的排序? Please help me. 请帮我。

You might be able to override the isSortable(int) method of TableRowSorter to prevent that column from being sorted: 您可以覆盖TableRowSorterisSortable(int)方法以防止对该列进行排序:

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);
  }
}

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 . 我建议你先阅读Swing教程中有关排序和过滤的部分

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. 将此代码作为起始代码,然后使用实际表数据自定义此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM