Component for filtering a list
What is the Java Swing component that would be suitable for creating a filterable list like seen below?
This type of filtering is most easily done using a single column JTable
. A table has inbuilt functionality to add a RowSorter
which:
..provides the basis for sorting and filtering.
See also How to Use Tables: Sorting and Filtering.
Here is an example for filtering the font family names:
On the left is a more 'list looking' component, while the right hand side shows a component that is clearly a table.
Code
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;
import javax.swing.text.Document;
import javax.swing.table.TableRowSorter;
public class FontFilter {
private JComponent ui = null;
JTextField filterText;
TableRowSorter sorter;
FontFilter(boolean listLike) {
initUI(listLike);
}
public void initUI(boolean listLike) {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
GraphicsEnvironment ge
= GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();
String[][] tableData = new String[fonts.length][1];
for (int i = 0; i < fonts.length; i++) {
tableData[i][0] = fonts[i];
}
String[] header = {"Fonts"};
JTable table = new JTable(tableData, header);
if (listLike) {
Dimension d = table.getPreferredScrollableViewportSize();
table.setPreferredScrollableViewportSize(new Dimension(d.width/2,d.height));
table.setShowGrid(false);
table.setTableHeader(null);
table.setFillsViewportHeight(true);
}
ui.add(new JScrollPane(table));
sorter = new TableRowSorter(table.getModel());
table.setRowSorter(sorter);
filterText = new JTextField(10);
ui.add(filterText, BorderLayout.PAGE_START);
Document doc = filterText.getDocument();
DocumentListener listener = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
newFilter();
}
@Override
public void removeUpdate(DocumentEvent e) {
newFilter();
}
@Override
public void changedUpdate(DocumentEvent e) {
newFilter();
}
};
doc.addDocumentListener(listener);
}
private void newFilter() {
RowFilter rf = null;
//If current expression doesn't parse, don't update.
try {
rf = RowFilter.regexFilter(filterText.getText(), 0);
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
sorter.setRowFilter(rf);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
FontFilter o1 = new FontFilter(true);
FontFilter o2 = new FontFilter(false);
JFrame f = new JFrame("Font Filter");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.add(o1.getUI(), BorderLayout.LINE_START);
f.add(o2.getUI(), BorderLayout.CENTER);
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
If you want or have to use only standard Swing components then the method described by @AndrewThompson is the way to go.
But if you are able to use third-party libraries then a good alternative is JXList
component included in SwingX project. This component is a JList
extension and provides the ability to sort and filter its content plus other interesting features (check SwingLabs Demos).
The following snippet is the basis to make it work:
JXList list = new JXList(listModel);
list.setAutoCreateRowSorter(true);
This is enough to create and install a RowSorter<ListModel, Integer>
instance as the list's row sorter which can be retrieved by calling getRowSorter()
method. The actual object returned by this method is a ListSortController
which inherits from DefaultRowSorter
and also implements the non-standard SortController
interface.
It is important to keep this class hierarchy in mind because it's possible to supply a RowFilter
in different ways. All the following alternatives assume the row sorter is auto-created.
Note: IMO the first method is the preferred one because we can delegate the dirty work of down-casting the row sorter to supply a row filter to the component.
1. Set the row filter directly on the list
list.setRowFilter(rowFilter);
This is a convenience method to set the row filter. However it is required by contract that the actual list's row sorter be a SortController
compliant instance. Otherwise the setRowFilter(...)
call has no effect.
2. Cast the row sorter as SortController
SortController<ListModel> sortController
= (SortController<ListModel>)list.getRowSorter();
sortController.setRowFilter(rowFilter);
The SortController
interface provides a method to set the row filter which is used to by-pass the row filter in the method # 1.
3. Cast the row sorter as DefaultRowSorter
DefaultRowSorter<ListModel, Integer> sorter
= (DefaultRowSorter<ListModel, Integer>)list.getRowSorter();
sorter.setRowFilter(rowFilter);
This method is the same than when we are working with JTable
.
Example
Here is a simple demo about filtering with JXList
. Once again please check SingLabs Demos for better examples.
import java.awt.BorderLayout;
import java.awt.GraphicsEnvironment;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.jdesktop.swingx.JXList;
public class FilterListDemo {
private JXList list;
private void createAndShowGui() {
final JTextField filterText = new JTextField(30);
filterText.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
FilterListDemo.this.createFilter(filterText.getText(), false);
}
@Override
public void removeUpdate(DocumentEvent e) {
FilterListDemo.this.createFilter(filterText.getText(), false);
}
@Override
public void changedUpdate(DocumentEvent e) {
FilterListDemo.this.createFilter(filterText.getText(), false);
}
});
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();
list = new JXList(fonts);
list.setAutoCreateRowSorter(true);
JPanel content = new JPanel(new BorderLayout(8,8));
content.add(filterText, BorderLayout.PAGE_START);
content.add(new JScrollPane(list), BorderLayout.CENTER);
content.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
JFrame frame = new JFrame("Filter List Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(content);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void createFilter(String text, final boolean caseSensitive) {
final String filterText = caseSensitive ? text : text.toUpperCase();
list.setRowFilter(new RowFilter<ListModel, Integer>() {
@Override
public boolean include(RowFilter.Entry<? extends ListModel
, ? extends Integer> entry) {
String entryValue = caseSensitive
? entry.getStringValue(0)
: entry.getStringValue(0).toUpperCase();
return filterText == null
|| filterText.trim().isEmpty()
|| entryValue.contains(filterText.trim());
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new FilterListDemo().createAndShowGui();
}
});
}
}