Results 1 to 1 of 1
  1. #1
    Rafiqul is offline Member
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    12
    Rep Power
    0

    Question Filtering rows in JTable

    I have manually written a java program which takes a database query from user and shows the desired rows in a JTable. User can also give a string in a textfield to filter the rows. Query works fine. But problem is with filtering. Here, I have only given the lines of code related with filtering:

    Java Code:
    JTable myTable= new JTable((TableModel) fc);
    
    final JTextField filterText = new JTextField();
    
    JButton filterButton = new JButton ("Apply Filter");
    
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel> ((TableModel) fc);
    /*Here fc is the object of a class where I have overridden getColumnCount(), getRowCount(),
    getValueAt() and getColumnName() methods of interface TableModel. object of a class where
    I have overridden getColumnCount(), getRowCount(), getValueAt() and getColumnName() methods
    of interface TableModel.*/
    
    myTable.setRowSorter(sorter);
    
    //Event handling code when the "Apply Filter" button is clicked
    filterButton.addActionListener(
    	new ActionListener()
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			String txt = filterText.getText();
    			sorter.setRowFilter(RowFilter.regexFilter("(?i)" + txt));
    		}
    	}
    );
    Filtering mechanism works fine. But when there is no row that match with the txt, one row (title row, which contains the column names) is shown. But I don't want to show the 'title row'. How can I do it? I also want to show a message dialog containing the message "No matching row found". But return type of setRowFilter() is void. How to determine that there is no matching row ?

    I searched the web but didn't find the desired solution. Please help. Thanks.
    Last edited by Rafiqul; 12-15-2013 at 07:05 PM.

Similar Threads

  1. JTable, put a listener in the rows !
    By Otyugh in forum New To Java
    Replies: 3
    Last Post: 12-31-2012, 08:54 PM
  2. JTable after filtering problem
    By fatpoo_001 in forum New To Java
    Replies: 1
    Last Post: 10-11-2011, 04:47 PM
  3. Jtable rows
    By riddhishah28 in forum AWT / Swing
    Replies: 3
    Last Post: 02-18-2011, 07:24 AM
  4. How to Add,Remove Rows in JTable
    By Dinesh_rockz in forum AWT / Swing
    Replies: 2
    Last Post: 02-03-2011, 06:30 AM
  5. Deleting All rows in the JTable
    By surot in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 11:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •