Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to search data in JTable using JTextField in Java Swing?

 


GUI
package Trial;

import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableRowSorter;

import Libs.CustomerLibs;
import Libs.JDBCCustomer;

class Demo {

	
	JTextField searchField;
	JButton btn;
	JTable table;
	DefaultTableModel modeltable;
	TableRowSorter sorter;
	

	Demo() {
		JFrame frame = new JFrame();
		frame.setTitle("Hancie e-Learning Studio");
		frame.setLayout(null);
		frame.setSize(1000, 500);
		frame.setLocationRelativeTo(null);

        
		
        modeltable = new DefaultTableModel();

		table = new JTable(modeltable);
		JTableHeader h1=table.getTableHeader();
		h1.setBackground(Color.black);
		h1.setForeground(Color.white);
		h1.setFont(new Font("Verdana", Font.BOLD,15));
		table.setFont(new Font("Verdana", Font.PLAIN,16));
		table.setRowHeight(20);
		table.setSelectionBackground(Color.BLACK);
		table.setSelectionForeground(Color.white);
		
		modeltable.addColumn("ID");
		modeltable.addColumn("Title");
		modeltable.addColumn("Name");
		modeltable.addColumn("Gender");
		modeltable.addColumn("DOB");
		modeltable.addColumn("Mobile");
		
		
		sorter = new TableRowSorter<>(modeltable);
		table.setRowSorter(sorter);
		
		JDBCCustomer jdbc2 = new JDBCCustomer();
		ArrayList select = jdbc2.select_all();
		if(select.size()>0) {
			for(int i=0; i<select.size(); i++) {
				CustomerLibs p = (CustomerLibs) select.get(i);
				Object []tmp= {p.getCustomer_ID(), 
						p.getTitle(), 
						p.getName(),
						p.getGender(),	
						p.getDOB(),
						p.getMobile()							
								
								
															
								

				};
				modeltable.addRow(tmp);
			}
		}
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(10,50,950,400);
		frame.add(scrollPane, BorderLayout.CENTER);
		
		searchField=new JTextField();
		searchField.setFont(new Font("Times New Roman",Font.PLAIN,18));
		searchField.setBounds(210,10,200,30);
		frame.add(searchField);
		
		searchField.getDocument().addDocumentListener
                (new DocumentListener() {
	         @Override
	         public void insertUpdate(DocumentEvent e) {
	            search(searchField.getText());
	         }
	         @Override
	         public void removeUpdate(DocumentEvent e) {
	            search(searchField.getText());
	         }
	         @Override
	         public void changedUpdate(DocumentEvent e) {
	            search(searchField.getText());
	         }
	         
	         
	         public void search(String str) {
	            if (str.length() == 0) {
	               sorter.setRowFilter(null);
	            } else {
	               sorter.setRowFilter(RowFilter.regexFilter(str));
	            }
	         }
	      });
		
		
		
		frame.setVisible(true);

	}

	public static void main(String args[]) {
		new Demo();
	}
}
Output
Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.