- 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 to implement the search functionality of a JTable in Java?
A JTable is a subclass of JComponent for displaying complex data structures. A JTable component can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable can generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener, RowSorterListener interfaces. We can implement the search functionality of a JTable by input a string in the JTextField, it can search for a string available in a JTable. If the string matches it can only display the corresponding value in a JTable. We can use the DocumentListener interface of a JTextField to implement it.
Example
import java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; public class JTableSearchTest extends JFrame { private JTextField jtf; private JLabel searchLbl; private TableModel model; private JTable table; private TableRowSorter sorter; private JScrollPane jsp; public JTableSearchTest() { setTitle("JTableSearch Test"); jtf = new JTextField(15); searchLbl = new JLabel("Search"); String[] columnNames = {"Name", "Technology"}; Object[][] rowData = {{"Raja", "Java"},{"Vineet", "Java Script"},{"Archana", "Python"},{"Krishna", "Scala"},{"Adithya", "AWS"},{"Jai", ".Net"}}; model = new DefaultTableModel(rowData, columnNames); sorter = new TableRowSorter<>(model); table = new JTable(model); table.setRowSorter(sorter); setLayout(new FlowLayout(FlowLayout.CENTER)); jsp = new JScrollPane(table); add(searchLbl); add(jtf); add(jsp); jtf.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { search(jtf.getText()); } @Override public void removeUpdate(DocumentEvent e) { search(jtf.getText()); } @Override public void changedUpdate(DocumentEvent e) { search(jtf.getText()); } public void search(String str) { if (str.length() == 0) { sorter.setRowFilter(null); } else { sorter.setRowFilter(RowFilter.regexFilter(str)); } } }); setSize(475, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); setResizable(false); setVisible(true); } public static void main(String[] args) { new JTableSearchTest(); } }
Output
- Related Articles
- How can we implement the word wrap JTableHeader of a JTable in Java?
- How to add Google Search Functionality in Kotlin?
- How to implement the opposite of INITCAP() functionality with MySQL?
- How can we implement cut, copy and paste functionality of JTextField in Java?
- How to add google search functionality in an android app?
- How to use search functionality in custom list view in Android?
- Java program to implement binary search
- Java program to implement linear search
- How to use search functionality in custom listview in Android using kotlin?
- How to implement a Keyword Search in MySQL?
- How to add Google Search Functionality in an Android App using Kotlin?
- How to change each column width of a JTable in Java?
- How to set magins between cells of a JTable in Java?
- How to select different cells of a JTable programmatically in Java?
- How to select the first column in a JTable with Java?

Advertisements