Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the filtered model?

I'm working with JTables to display information that users can filter, and if the user saves after filtering I want to save the filtered table to a textfile for persistence (meaning anything that got filtered out will not be saved to the textfile).

For filtering I just followed the filtering part of this tutorial: http://download.oracle.com/javase/tutorial/uiswing/components/table.html#sorting and it works fine, but I'm not sure of any way that I can get a model of the current display as opposed to the underlying model that contains everything that hasn't been filtered out.

Is there any way to do this with this with the way that I'm filtering?

Thanks!

like image 436
f4ngy Avatar asked Mar 25 '11 20:03

f4ngy


2 Answers

Ask the table its number of rows (using getRowCount()), which will return the number of filtered (visible) rows. Iterate from 0 to the rowCount, convert each row index to the model index using convertRowIndexToModel(), and ask your model the data at each model index to build the list of filtered (visible) data.

like image 120
JB Nizet Avatar answered Oct 21 '22 22:10

JB Nizet


This code shows how to do this. Please note that B row is not printed to the input after the button is pressed.

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JTableFilterDemo {
    public static void main(String[] args) {
        Object[][] data = { { "A", 5 }, { "B", 2 }, { "C", 4 }, { "D", 8 } };
        String columnNames[] = { "Item", "Value" };
        TableModel model = new DefaultTableModel(data, columnNames) {
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        JTable table = new JTable(model);

        RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
            public boolean include(Entry entry) {
                Integer population = (Integer) entry.getValue(1);
                return population.intValue() > 3;
            }
        };

        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
                model);
        sorter.setRowFilter(filter);
        table.setRowSorter(sorter);
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame("Filtering Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton btnNewButton = new JButton("Print values");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                for(int row = 0;row < table.getRowCount();row++) {
                    System.out.println(table.getModel().getValueAt(table.convertRowIndexToModel(row), 0));
                }
            }
        });
        frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);
        frame.getContentPane().add(scrollPane);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}
like image 34
Radim Burget Avatar answered Oct 21 '22 21:10

Radim Burget