Setting up Swing JTable for filtering / searching

I would like to add filtering / searching ability for all columns (so this is generic), logic is not a problem. However, I thought the best way to do this from a visualization standpoint is to have a textbox above each column header (or part of a column header)?

I am having trouble trying to achieve this:

  • Embedding inside column header requires injecting a new renderer + there is a problem that somehow I need to catch the event because the cell is being drawn and not responding to events. Sounds like me overkill.
  • Having a separate row (panel) of textbox components above each column sounds much easier at first glance, however aligning the textbox with columns is problematic since I haven't found an API in JTable

    or TableColumn

    that returns the column coordinates. Moreover, moving around the column would also mean setting up the text boxes (which is possible, but another service message).

Am I missing something or is it really difficult to achieve something this simple? Any other suggestions?

+3


source to share


3 answers


There is a library for your first approach. It integrates easily and works great: Swing Bits



+3


source


of visualization is having a textfield above each column header 
(or part of the column header)... 

      



+2


source


See below example for filtering based on textbox entries filtering your Jtable data rely on textbox entry:

import java.awt.BorderLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

      

public class TestTableSorterFilter extends JApplet {

private String[] columnNames
        = {"Country", "Capital", "Population in Millions", "Democracy"};

private Object[][] data = {
    {"USA", "Washington DC", 280, true},
    {"Canada", "Ottawa", 32, true},
    {"United Kingdom", "London", 60, true},
    {"Germany", "Berlin", 83, true},
    {"France", "Paris", 60, true},
    {"Norway", "Oslo", 4.5, true},
    {"India", "New Delhi", 1046, true}
};

private JTable jTable = new JTable(data, columnNames);

private TableRowSorter<TableModel> rowSorter
        = new TableRowSorter<>(jTable.getModel());

private JTextField jtfFilter = new JTextField();
private JButton jbtFilter = new JButton("Filter");

public TestTableSorterFilter() {
    jTable.setRowSorter(rowSorter);

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(new JLabel("Specify a word to match:"),
            BorderLayout.WEST);
    panel.add(jtfFilter, BorderLayout.CENTER);

    add(panel, BorderLayout.SOUTH);
    add(new JScrollPane(jTable), BorderLayout.CENTER);

    jtfFilter.getDocument().addDocumentListener(new DocumentListener(){

        @Override
        public void changedUpdate(DocumentEvent arg0) {}

        @Override
        public void insertUpdate(DocumentEvent arg0) {
            String text = jtfFilter.getText();

            if (text.trim().length() == 0) {
                rowSorter.setRowFilter(null);
            } else {
                rowSorter.setRowFilter(RowFilter.regexFilter(text));
            }   
        }

        @Override
        public void removeUpdate(DocumentEvent arg0) {
            String text = jtfFilter.getText();
            if (text.trim().length() == 0) {
                rowSorter.setRowFilter(null);
            } else {
                rowSorter.setRowFilter(RowFilter.regexFilter(text));
            }   
        }
    });
}
}

      

0


source







All Articles