views:

19

answers:

2

I have a standard JTable with certain data inside. I would like to create an automatic filtering function: as the user types in a specific textfield the words the filtering process is started automatically, without the need of a clicking a button. Is it possible in Java Swing?

A: 

Yes its possible. Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables", where you will find a working example of filtering that does this.

camickr
+1  A: 

Yes, it is possible!

Please visit the website for the SwingSet3 showcase: https://swingset3.dev.java.net/

Click on the orange WebStart Launch Button to start the application. In the Data category there is a JTable demo listing Oscar candidates and winners.

The TextField with the title "Search Titles and Recipients" is exactly doing what you want. The SwingSet3 Demo even lets you highlight the code snippets via the ComboBox-entry "Setup search filter".

The RowFilter in the first snippet ignores leading "The "s and "A "s to match more movie titles. I would replace the RegEx pattern with this

".*" + filterString + ".*" 

to get a real "find everything while I type" user experience.

The second snippet shows you how to implement a DocumentListener to refresh the table filter on every keystroke.

bobndrew