Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8253539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:54:35+00:00 2026-06-08T00:54:35+00:00

Now I have a search textField implemented with KeyReleased event, which don’t found/update jTable

  • 0

Now I have a search textField implemented with KeyReleased event, which don’t found/update jTable when I start typing for example “An” from “Andrew”, and just after I finish to type exactly (caseSensitive) the name which I want to find.

So, what I want is to implement the Filtering method from this site but I have big problems. After below implementation and removing the “old” KeyReleased event, when I type something in textfield “txt_search” nothing happens with jTable.

My code of table generated by IDE (NetBeans) is (copied from customize code panel):

Table_Employee = new javax.swing.JTable();

Table_Employee.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
        {null, null, null, null},
        {null, null, null, null},
        {null, null, null, null},
        {null, null, null, null}
    },
    new String [] {
        "Title 1", "Title 2", "Title 3", "Title 4"
    }
)
);

Table_Employee.setToolTipText("Employee info table");

Table_Employee.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

Table_Employee.getTableHeader().setReorderingAllowed(false);

Table_Employee.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        Table_EmployeeMouseClicked(evt);
    }
});
Table_Employee.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyPressed(java.awt.event.KeyEvent evt) {
        Table_EmployeeKeyPressed(evt);
    }
});

jScrollPane1.setViewportView(Table_Employee);

and I added this on my panel class where table is:

import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;


public class Employee_info extends javax.swing.JFrame {
Connection conn=null;
ResultSet rs=null;
PreparedStatement pst=null;
private TableRowSorter<DefaultTableModel> sorter;
    /**
     * Creates new form Employee_info
     */
    public Employee_info() {
        initComponents();
        conn=javaconnect.ConnecrDb();
        Update_table();
        Fillcombo();
        currentDate();
        Table_Employee.setAutoCreateRowSorter(true);
        Table_Employee.setFillsViewportHeight(true);
        Table_Employee.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


        DefaultTableModel model = new DefaultTableModel();
        sorter = new TableRowSorter<DefaultTableModel>(model);
        Table_Employee = new JTable(model);


        txt_search = new JTextField();
        //Whenever filterText changes, invoke newFilter.
        txt_search.getDocument().addDocumentListener(
                new DocumentListener() {
                    public void changedUpdate(DocumentEvent e) {
                        newFilter();
                    }
                    public void insertUpdate(DocumentEvent e) {
                        newFilter();
                    }
                    public void removeUpdate(DocumentEvent e) {
                        newFilter();
                    }
                });
    }

    private void newFilter() {
    RowFilter<DefaultTableModel, Object> rf = null;
    //If current expression doesn't parse, don't update.
    try {
        rf = RowFilter.regexFilter(txt_search.getText(), 0);
    } catch (java.util.regex.PatternSyntaxException e) {
        return;
    }
    sorter.setRowFilter(rf);
}

I really need some help, please! This is my last chance, because I really made the google search solutions, and nothing changed.

With respect,
Andrew

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:54:38+00:00Added an answer on June 8, 2026 at 12:54 am
    • little modiefied code from JTable tutorial about filtering and sorting

    • for filtering for JCheckBox you can to put “true” / “false”

    code

    import java.awt.*;
    import java.util.regex.PatternSyntaxException;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    
    public class TableFilterSorter extends JPanel {
    
        private boolean DEBUG = false;
        private static final long serialVersionUID = 1L;
    
        public TableFilterSorter() {
            super(new BorderLayout(5, 5));
            final JTextField filterCpText = new JTextField();
            filterCpText.setFont(new Font("Serif", Font.BOLD, 28));
            filterCpText.setForeground(Color.BLUE);
            filterCpText.setBackground(Color.LIGHT_GRAY);
            JPanel filterCpPanel = new JPanel();
            filterCpPanel.setLayout(new BorderLayout(5, 5));
            filterCpPanel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
            filterCpPanel.setBackground(Color.LIGHT_GRAY);
            filterCpPanel.setPreferredSize(new Dimension(300, 30));
            filterCpPanel.add(filterCpText, BorderLayout.CENTER);
            add(filterCpPanel, BorderLayout.NORTH);
            final JTable table = new JTable(new MyTableModel());
            table.setPreferredScrollableViewportSize(new Dimension(500, 160));
            table.setFillsViewportHeight(true);
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane, BorderLayout.CENTER);
            TableModel myTableModel = table.getModel();
            final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(myTableModel);
            table.setRowSorter(sorter);
            filterCpText.getDocument().addDocumentListener(new DocumentListener() {
    
                private void searchFieldChangedUpdate(DocumentEvent evt) {
                    String text = filterCpText.getText();
                    if (text.length() == 0) {
                        sorter.setRowFilter(null);
                        table.clearSelection();
                    } else {
                        try {
                            sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text, 4));
                            table.clearSelection();
                        } catch (PatternSyntaxException pse) {
                            JOptionPane.showMessageDialog(null, "Bad regex pattern",
                                    "Bad regex pattern", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
    
                @Override
                public void insertUpdate(DocumentEvent evt) {
                    searchFieldChangedUpdate(evt);
                }
    
                @Override
                public void removeUpdate(DocumentEvent evt) {
                    searchFieldChangedUpdate(evt);
                }
    
                @Override
                public void changedUpdate(DocumentEvent evt) {
                    searchFieldChangedUpdate(evt);
                }
            });
        }
    
        private class MyTableModel extends AbstractTableModel {
    
            private static final long serialVersionUID = 1L;
            private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
            private Object[][] data = {
                {"Mary", "Campione", "Snowboarding", new Integer(5), false},
                {"Alison", "Huml", "Rowing", new Integer(3), true},
                {"Kathy", "Walrath", "Knitting", new Integer(2), false},
                {"Sharon", "Zakhour", "Speed reading", new Integer(20), true},
                {"Philip", "Milne", "Pool", new Integer(10), false},
                {"Mary", "Campione", "Snowboarding", new Integer(5), false},
                {"Alison", "Huml", "Rowing", new Integer(3), true},
                {"Kathy", "Walrath", "Knitting", new Integer(2), false},
                {"Sharon", "Zakhour", "Speed reading", new Integer(20), true},
                {"Philip", "Milne", "Pool", new Integer(10), false},};
    
            @Override
            public int getColumnCount() {
                return columnNames.length;
            }
    
            @Override
            public int getRowCount() {
                return data.length;
            }
    
            @Override
            public String getColumnName(int col) {
                return columnNames[col];
            }
    
            @Override
            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
    
            @Override
            public Class<?> getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }
    
            @Override
            public boolean isCellEditable(int row, int col) {
                if (col < 2) {
                    return false;
                } else {
                    return true;
                }
            }
    
            @Override
            public void setValueAt(Object value, int row, int col) {
                if (DEBUG) {
                    System.out.println("Setting value at " + row + "," + col
                            + " to " + value + " (an instance of " + value.getClass() + ")");
                }
                data[row][col] = value;
                fireTableCellUpdated(row, col);
                if (DEBUG) {
                    System.out.println("New value of data:");
                    printDebugData();
                }
            }
    
            private void printDebugData() {
                int numRows = getRowCount();
                int numCols = getColumnCount();
                for (int i = 0; i < numRows; i++) {
                    System.out.print("    row " + i + ":");
                    for (int j = 0; j < numCols; j++) {
                        System.out.print("  " + data[i][j]);
                    }
                    System.out.println();
                }
                System.out.println("--------------------------");
            }
        }
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("TableDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            TableFilterSorter newContentPane = new TableFilterSorter();
            newContentPane.setOpaque(true);
            frame.setContentPane(newContentPane);
            frame.setLocation(150, 150);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on Android Application. Now I have requirement of search the entered
My goal for now is to have a user search for other users who
I am altering the question now! I have the search working using the following:
I have been using solr search and now I am adding pagination via the
i have written this tree class for a familytree now i need a search
I have to implement a search algorithm for a school assignment. Right now, i'm
I have asked What should i know about search engine crawling? Now i would
I have a project using Lucene3.5 already. Now i need to provide web search
I have a DataTable X, if I now want to search for a certain
I now have a running Java program which only lacks of the final step,that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.