- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we filter a JTable in Java?
A JTable provides a very flexible possibility to create and display tables. The TableModel interface defines methods for objects that specify the contents of a table. The AbstractTableModel class is typically extended to provide a custom implementation of a model table. A JTable class provides the ability to edit tables using the method setCellEditor() allows an object of the TableCellEditor interface.
We can filter a table using the setRowFilter() method of TableRowSorter class.
Example
import java.awt.*; import java.awt.event.*; import java.util.regex.*; import javax.swing.*; import javax.swing.table.*; public class FilterTableTest extends JFrame { private JTable table; private TableModel model; public FilterTableTest() { setTitle("FilterTable Test"); Object rows[][] = {{"Adithya", "Content Developer", 25000}, {"Jai", "SME", 30000}, {"Chaitanya", "Java Engineer", 45000}, {"Ramesh", "Scala Developer", 40000}, {"Ravi", "SAP Consultant", 70000}}; Object columns[] = {"Name", "Designation", "Salary"}; model = new DefaultTableModel(rows, columns) { public Class getColumnClass(int column) { Class returnValue; if((column >= 0) && (column < getColumnCount())) { returnValue = getValueAt(0, column).getClass(); } else { returnValue = Object.class; } return returnValue; } }; table = new JTable(model); final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); table.setRowSorter(sorter); add(new JScrollPane(table), BorderLayout.CENTER); JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel("Filter"); panel.add(label, BorderLayout.WEST); final JTextField filterText = new JTextField(""); panel.add(filterText, BorderLayout.CENTER); add(panel, BorderLayout.NORTH); JButton button = new JButton("Filter"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = filterText.getText(); if(text.length() == 0) { sorter.setRowFilter(null); } else { try { sorter.setRowFilter(RowFilter.regexFilter(text)); } catch(PatternSyntaxException pse) { System.out.println("Bad regex pattern"); } } } }); add(button, BorderLayout.SOUTH); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String args[]) { new FilterTableTest(); } }
Output
- Related Articles
- How can we remove a selected row from a JTable in Java?
- How can we sort a JTable on a particular column in Java?
- How can we disable the cell editing inside a JTable in Java?
- How can we add/insert a JButton to JTable cell in Java?
- How can we add/insert a JCheckBox inside a JTable cell in Java?
- How can we implement the word wrap JTableHeader of a JTable in Java?
- How can we show/hide the table header of a JTable in Java?
- How can we prevent the re-ordering columns of a JTable in Java?
- How can we add/insert a JRadioButton to a JTable cell in Java?\n
- Can we hide the table header from a JTable in Java?
- How can we detect the double click events of a JTable row in Java?
- How can we set the background/foreground color for individual column of a JTable in Java?
- How can we create a LOG filter for Logging purposes in C# ASP.NET WebAPI?
- How can we filter data with the help of MySQL subquery?
- How can we stop a thread in Java?

Advertisements