且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

选中了用于过滤JTable的按钮

更新时间:2023-12-03 18:11:16

Let's revisit the tutorial How to Use Tables: Sorting and Filtering.

In addition to reordering the results, a table sorter can also specify which rows will be displayed. This is known as filtering. TableRowSorter implements filtering using javax.swing.RowFilter objects. RowFilter implements several factory methods that create common kinds of filters.

Things to look at:

  • TableRowSorter. - We know we're going to need this.
  • Oh, they gave us a link to RowFilter docs. Let's open that up in a new browser tab.
  • But first the last line "RowFilter implements several factory methods that create common kinds of filters.". We should probably look to see what those "common kinds" are.

RowFilter javadoc

  • Static methods - andFilter, dateFilter, notFilter, numberFilter, orFilter, regexFilter. Ok, I recognize the regexFilter from the tutorial. But those others, just by their names alone, don't sound too promising. Maybe we should implement our own. Let's look at the rest of the javadoc

javax.swing.RowFilter<M,I>

Type Parameters:
M - the type of the model; for example PersonModel
I - the type of the identifier; when using TableRowSorter this will be Integer

Ok so from this we know two thing. When we create our custom RowFilter, we should pass a TableModel type as the first parameter, and type Integer to represent the row number.

RowFilter is used to filter out entries from the model so that they are not shown in the view. For example, a RowFilter associated with a JTable might only allow rows that contain a column with a specific string. The meaning of entry depends on the component type. For example, when a filter is associated with a JTable, an entry corresponds to a row; when associated with a JTree, an entry corresponds to a node.

Subclasses must override the include method to indicate whether the entry should be shown in the view. The Entry argument can be used to obtain the values in each of the columns in that entry

So based on those two paragraghs, we know the RowFilter is used to filter out rows we don't want in the view, and that if we create our own, we will need to override the include method to return whether a row should be shown or not. And also the include method will have an Entry argument, from which we can obtain details that may be o interest to us, when determining which row to not return true on. Here's the basic example (guideline) the javadoc gives us

RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() {
    @Override
    public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
        // do your logic here to return true or false.
    }
};

Cool, but what is this RowFilter.Entry class, and what interesting information can it provide to us? Let's look at the javadoc.

One interesting thing we can see is the getModel() method. From that we can get our table model. So let's finish the implementation

private static final int CHECKBOX_COLUMN = 4;
...
RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() {
    @Override
    public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
        int modelRow = entry.getIdentifier();
        Boolean checked = (Boolean)entry.getModel().getValueAt(modelRow, CHECKBOX_COLUMN);
        return checked;
    }
};

Not much too it. Now all that's left is setting the filter to the sorter, as you learned from the Oracle tutorial already, using the regex filter.

Here's a fill demo code. It doesn't implement the ActionListener for the button press. I didn't want to do everything for you. You should have enough information to handle that on your own :-D Happy Learning!

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class TableBooleanFilterDemo {

    public TableBooleanFilterDemo() {
        JTable table = getTable();
        TableRowSorter<TableModel> rowSorter = new TableRowSorter<>(table.getModel());
        RowFilter filter = getRowFilter();
        rowSorter.setRowFilter(filter);
        table.setRowSorter(rowSorter);

        JFrame frame = new JFrame();
        frame.add(new JScrollPane(table));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private static final int CHECKBOX_COLUMN = 4;

    private RowFilter getRowFilter() {
        RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() {
            @Override
            public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
                int modelRow = entry.getIdentifier();
                Boolean checked = (Boolean)entry.getModel().getValueAt(modelRow, CHECKBOX_COLUMN);
                return checked;
            }
        };
        return filter;
    }

    private JTable getTable() {
        Object[][] data = {
            {"Kathy", "Smith",
                "Snowboarding", new Integer(5), new Boolean(false)},
            {"John", "Doe",
                "Rowing", new Integer(3), new Boolean(true)},
            {"Sue", "Black",
                "Knitting", new Integer(2), new Boolean(false)},
            {"Jane", "White",
                "Speed reading", new Integer(20), new Boolean(true)},
            {"Joe", "Brown",
                "Pool", new Integer(10), new Boolean(false)}
        };
        String[] columnNames = {"First Name",
            "Last Name",
            "Sport",
            "# of Years",
            "Vegetarian"};
        return new JTable(new DefaultTableModel(data, columnNames) {
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new TableBooleanFilterDemo();
            }
        });
    }
}

To be honest, I've never had to implement my own RowFilter, but the above is the process I pretty much go through when trying to learn something new. Took me longer to write this answer, then to learn how to do it. Gotta learn to read documentation, not just tutorials. Tutorials are a good starting point, but once you get to more complicated matters, you need to dig deeper. Same with any new technology you want to learn.