Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3403772
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:18:05+00:00 2026-05-18T05:18:05+00:00

I have a JTable with four columns, the first one containing either a number

  • 0

I have a JTable with four columns, the first one containing either a number or a text, the other three only text. I’m trying to filter this table with the help of a RowFilter:

sorter = new TableRowSorter<TableModel>(myOwnTableModel);

The checkboxFilter I got works well enough:

sorter.setRowFilter(RowFilter.regexFilter("^[0-9]$", 0));

This sorter is activated or deactivate depending on a checkbox that is either set or not.
The second filtering happens if a user puts some text in a textfield. For itself, this works fine already:

String regex = "(?i)" + Pattern.quote(s); // s = input Text of user
sorter.setRowFilter(RowFilter.regexFilter(regex, 1,2,3));

What I can’t do, is to activate both filters at the same time. Maybe I’m thinking way too far, my idea has been to “concatenate” the two filters, the checkboxFilter should be “and” the other “or”. I tried several things, to me the most promising looked something like:

String regex = "(?i)" + Pattern.quote(s);
bookFilter = RowFilter.regexFilter(regex, 1,2,3);
sorter.setRowFilter(bookFilter.andFilter(Arrays.asList(
                    RowFilter.regexFilter("^[0-9]$", 0))));

Unfortunately, this doesn’t lead to any usable result. Any ideas appreciated 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:18:06+00:00Added an answer on May 18, 2026 at 5:18 am

    The solution is to add an ActionListener to the JCheckBox to update the filter state if the checkbox is toggled and to add a DocumentListener to the JTextField‘s underlying Document to update the filter state if the contents of the field is updated.

    The other bug in your code is that you are calling the static andFilter method on your bookFilter instance and are only passing in the newly constructed regex filter (i.e. you are only passing in one parameter to andFilter). The correct usage is:

    RowFilter andFilter = RowFilter.andFilter(filter1, filter2, etc);
    

    Example Event Listeners

    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(); }
    });
    

    … 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.

    Example Filter Update Method

    public void updateFilters() {
      if (cb.isSelected()) {
        if (tf.getText().length() > 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() > 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.
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.