Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The solution is to add an <code>ActionListener</code> to the <code>JCheckBox</code> to update the filter state if the checkbox is toggled and to add a <code>DocumentListener</code> to the <code>JTextField</code>'s underlying Document to update the filter state if the contents of the field is updated.</p> <p>The other bug in your code is that you are calling the static <code>andFilter</code> method on your <code>bookFilter</code> instance and are only passing in the newly constructed regex filter (i.e. <strong>you are only passing in one parameter to <code>andFilter</code></strong>). The correct usage is:</p> <pre><code>RowFilter andFilter = RowFilter.andFilter(filter1, filter2, etc); </code></pre> <p><strong>Example Event Listeners</strong></p> <pre><code>JCheckBox cb = ... cb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { updateFilters(); } }); JTextField tf = ... tf.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) { updateFilters(); } public void removeUpdate(DocumentEvent e) { updateFilters(); } publci void changedUpdate(DocumentEvent e) { updateFilters(); } }); </code></pre> <p>... and then define your updateFilters() method to install a new filter based on when the checkbox is selected and whether the text field is empty or not.</p> <p><strong>Example Filter Update Method</strong></p> <pre><code>public void updateFilters() { if (cb.isSelected()) { if (tf.getText().length() &gt; 0) { // Both filters active so construct an and filter. sorter.setRowFilter(RowFilter.andFilter(bookFilter, checkBoxFilter)); } else { // Checkbox selected but text field empty. sorter.setRowFilter(checkBoxFilter); } } else if (tf.getText().length() &gt; 0) { // Checkbox deselected but text field non-empty. sorter.setRowFilter(bookFilter); } else { // Neither filter "active" so remove filter from sorter. sorter.setRowFilter(null); // Will cause table to re-filter. } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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