Question

  //Registration filter
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(databaseModel);
    DocumentListener idListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent documentEvent) {

        }
        public void insertUpdate(DocumentEvent documentEvent) {
            String searchText = txtReg.getText();
            if (searchText.length() == 0) {
              sorter.setRowFilter(null);
            } else {
              sorter.setRowFilter(RowFilter.regexFilter(searchText));
            }
            numberOfRecords.setText(searchTable.getRowCount() + " records");
        }
        public void removeUpdate(DocumentEvent documentEvent) {

            }


    };
   txtReg.getDocument().addDocumentListener(idListener);

I'm here again with probably silly questions... :( I have a JTable I wish to filter out as I type on a textfield. After researching a bit I came up with this, but it doesn´t work! If I put the filter outside the listener with an arbitrary string as search term it works perfectly. What am I missing? Thank you!

public class GUI extends JFrame {
private static final long serialVersionUID = -7968397937566078184L;
private static final JLabel numberOfRecords = new JLabel();
private static int numberOfRows;
private static DefaultTableModel databaseModel;



public GUI(DefaultTableModel database){
    setDefaultLookAndFeelDecorated(true);
    setLayout(new MigLayout());
    databaseModel = database;
    //Menus
    JMenuBar menubar = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenuItem update = new JMenuItem("Look for database updates");
    JMenuItem settings = new JMenuItem("Settings");
    JMenuItem exit = new JMenuItem("Exit");
    menubar.add(file);
    file.add(update);
    file.add(settings);
    file.add(exit);
    setJMenuBar(menubar);
     //Top section
    JPanel searchSection = new JPanel();
    searchSection.setLayout(new MigLayout("w 50mm!"));
    final JTextField txtReg = new JTextField(10);
    JLabel searchID = new JLabel("Current registration:");
    JLabel lblManufacturer = new JLabel("Manufacturer");
    JComboBox cmbManufacturer = new JComboBox();
    JLabel lblType = new JLabel("Type");
    JComboBox cmbType = new JComboBox();
    //Build main window
    searchSection.add(searchID);
    searchSection.add(txtReg, "span 2");
    searchSection.add(lblManufacturer);
    searchSection.add(cmbManufacturer, "growx, span 2");
    searchSection.add(lblType);
    searchSection.add(cmbType,"growx, span 2");
    //Table
    final JTable searchTable = new JTable(databaseModel);
    //Pack table, still have to figure this one out
    int width = 0;
     for (int row = 0; row < searchTable.getRowCount(); row++) {
         TableCellRenderer renderer = searchTable.getCellRenderer(row, 2);
         Component comp = searchTable.prepareRenderer(renderer, row, 2);
         width = Math.max (comp.getPreferredSize().width, width);
     }

    searchTable.getColumn("Key").setMaxWidth(0);
    searchTable.getColumn("Key").setMinWidth(0);
    searchTable.setRowSelectionAllowed(true);
    searchTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    searchTable.setAutoCreateRowSorter(true);
    searchTable.getRowSorter().toggleSortOrder(1);
    searchTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    numberOfRows=searchTable.getRowCount();
    JScrollPane browserSP = new JScrollPane(searchTable);
    browserSP.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4));
    setTitle("AirBase");
    setSize(1300,700);
    setMinimumSize(new Dimension(1250,650));
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    //TODO Fill manufacturer box

    //Registration filter
    //TODO Fix this!
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(databaseModel);
    DocumentListener idListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent documentEvent) {

        }
        public void insertUpdate(DocumentEvent documentEvent) {
            String searchText = txtReg.getText();
            if (searchText.length() == 0) {
              sorter.setRowFilter(null);
            } else {
              sorter.setRowFilter(RowFilter.regexFilter(searchText));
            }
            numberOfRecords.setText(searchTable.getRowCount() + " records");
        }
        public void removeUpdate(DocumentEvent documentEvent) {

            }


    };
   txtReg.getDocument().addDocumentListener(idListener);
    add(searchSection, "w 100%, h 5%, wrap");
    add(browserSP,"w 100%, h 95%, wrap");
    numberOfRecords.setFont(numberOfRecords.getFont().deriveFont(9.0f));
    numberOfRecords.setText(numberOfRows + " RECORDS");
    add(numberOfRecords, "alignx center");
}



}
Was it helpful?

Solution

So I ended up getting it started. Looks like a simple mistake. You forgot the setRowSorter for the table.

searchTable.setRowSorter(sorter);

Added that code an it works for me. Should work for you two, give you have a valid TableModel passed to the GUI. I just made up a dummy one.

NOTES:

  • You'll probably want to add the filter code to removeUpdate also, in the DocumentListener so when text is backspace'd, it will still filter.

  • This searchTable.getColumn("Key").setMaxWidth(0); makes the row data not visible. Is that what you really want?

  • If you want the filter to filter without case-sensitivity, you can use this

    sorter.setRowFilter(RowFilter.regexFilter("(?i)" + searchText));
    

    The "(?i)" is the case-insentive regex "flag"

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