Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have implemented a simple <code>TableFilterPanel</code> in the past which has one <code>JTextField</code> per table column and performs regular expression matching when text is present in a given field. I typically lay this out as a list of vertical labels + text fields (i.e. so it's fairly compact).</p> <p>My key class is called <code>ColumnSearcher</code>, which offers the ability to manufacture a <code>RowFilter</code> using the contents of the <code>JTextField</code>:</p> <pre><code>protected class ColumnSearcher { private final int[] columns; private final JTextField textField; public ColumnSearcher(int column, JTextField textField) { this.columns = new int[1]; this.textField = textField; this.columns[0] = column; } public JTextField getTextField() { return textField; } public boolean isEmpty() { String txt = textField.getText(); return txt == null || txt.trim().length() == 0; } /** * @return Filter based on the associated text field's value, or null if the text does not compile to a valid * Pattern, or the text field is empty / contains whitespace. */ public RowFilter&lt;Object, Object&gt; createFilter() { RowFilter&lt;Object, Object&gt; ftr = null; if (!isEmpty()) { try { ftr = new RegexFilter(Pattern.compile(textField.getText(), Pattern.CASE_INSENSITIVE), columns); } catch(PatternSyntaxException ex) { // Do nothing. } } return ftr; } } </code></pre> <p>When I wish to change the filter settings I build an "and" filter from each individual filter:</p> <pre><code>protected RowFilter&lt;Object, Object&gt; createRowFilter() { RowFilter&lt;Object, Object&gt; ret; java.util.List&lt;RowFilter&lt;Object, Object&gt;&gt; filters = new ArrayList&lt;RowFilter&lt;Object, Object&gt;&gt;(columnSearchers.length); for (ColumnSearcher cs : columnSearchers) { RowFilter&lt;Object, Object&gt; filter = cs.createFilter(); if (filter != null) { filters.add(filter); } } if (filters.isEmpty()) { ret = NULL_FILTER; } else { ret = RowFilter.andFilter(filters); } return ret; } </code></pre> <p>Typically I fire a <code>PropertyChangeEvent</code> when I wish to update the filters and have a PropertyChangeListener respond to it and rebuild my aggregate filter. You may then choose to fire the "rowFilter" <code>PropertyChangeEvent</code> if the user types in one of the text fields (e.g. by adding a <code>DocumentListener</code> to each <code>JTextField</code>).</p> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload