Question

I have a JTable, it contains a custom AbstractTableModel that return an object when getValueAt is called. And of course I have a custom TableCellRenderer that is capable of getting/constructing the text from the object so that it can be displayed.

However now I would like to write a filter. Filter will be a simple toggle button. When it is turned on I would like the filter to be applied and when it is turned off I would like filter to be removed.

I have two problems due to that.

First one is that I have absolutely no idea how to write a filter when you have to filter by object rather than a primitive.

Second is I have no idea how to attached the said filter to the toggle button to be able to turn it on and off.

I am sorry if this is a retarded question but http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting was the most useless documentation I saw since explanation was not in depth.

Thanks to anyone for their help.

EDIT: The object contains multiple fields, but I am interested in two filter toggles specifically. One returns a boolean value when I say isSuper, and the second return a string when I call getName. If first toggle is turned on it should show all entries that return true on isSuper, and the second toggle should show all entries where name is compromised of two words (space is present) when I call getName.

Was it helpful?

Solution

To be honest, the JavaDocs spell it out quite well...

With such little information to go on, the best you can hope for is an overview...

TableRowSorter<TableModel> trs = new TableRowSorter<TableModel>();
table.setRowSorter(trs);

// Create the row filder...
trs.setRowFilter(new RowFilter<TableModel, Integer>() {

    @Override
    public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
        boolean include = false;
        // Returns the value for the specific column...
        CustomObject value = (CustomObject)entry.getValue(filterColumn);
        if (filterBySuper) {
            include = value.isSuper();
        } else {
            include = value.getName().startsWith(fistPart) && value.getName().endWith(lastPart);
        }
        return include;
    }
});

When you want to update the filter, you simply need to call...

trs.sort();

OTHER TIPS

First one is that I have absolutely no idea how to write a filter when you have to filter by object rather than a primitive.

Did you read the RowFilter API? It shows an example of how to create a custom filter based on a custom Object.

Second is I have no idea how to attached the said filter to the toggle button to be able to turn it on and off.

Did you read the tutorial and try running the demo? The tutorial uses a DocumentFilter to change the filter dynamically every time the user changes the text in the text field. So the tutorial shows you how to dynamically changes the filter based on user input. You can modify the code to change the filter every time the toggle button is pressed.

it contains a custom AbstractTableModel that return an object when getValueAt is called. And of course I have a custom TableCellRenderer that is capable of getting/constructing the text from the object so that it can be displayed.

Unrelated to my answer, but I don't really understand that statement. Are you saying every cell in the model returns a differently object, or does every cell return the same object but you just display a different property of the object for column1, column2, column3 etc? Either way it sounds like a weird design. We can probably suggest something better. Post your SSCCE that demonstrates the problem.

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