1

I am doing a computing project which requires me to create a JTable which one could filter through. I have managed to add a sort function but I am unable to add a filter function. The closest example I could find, which is similar to what I am wanting, is the TableFilterDemoProject with the ability to launch it and the source code at http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#TableFilterDemo

I am trying to add the ability to filter my code to this piece of code

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.lang.*;
//////////////////
import javax.swing.RowFilter;
import javax.swing.event.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;
import java.awt.Dimension;
import java.awt.Component;
//////////////////


public class CompFrame
{

JFrame myMainWindow = new JFrame("This is my title");

JPanel  firstPanel = new JPanel(); //a panel for first tab

//first panel components
JScrollPane myScrollTable;


public void runGUI()
{
    myMainWindow.setBounds(10, 10, 1296, 756); //set position, then dimensions
    myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    myMainWindow.setLayout(new GridLayout(1,1));


    createFirstPanel(); //call method to create each panel

    myMainWindow.getContentPane().add(firstPanel); //adds the tabbedpane to mainWindow

    myMainWindow.setVisible(true); //make the GUI appear
}

public void createFirstPanel()
{
    firstPanel.setLayout(null);



    String[] aHeaders = {"Athlete ID","Forename","Surname"}; 
    String[][] sampleData = new String[3][3]; //rows,cols   
    sampleData[0][0] = "JS98";   
    sampleData[0][1] = "John";   
    sampleData[0][2] = "Smith";
    sampleData[1][0] = "DB56";   
    sampleData[1][1] = "David";   
    sampleData[1][2] = "Bower";
    sampleData[2][0] = "LL86";   
    sampleData[2][1] = "Lex";   
    sampleData[2][2] = "Luthor";





    JTable myTable = new JTable(sampleData,aHeaders);

    myTable.setAutoCreateRowSorter(true); //Sorts by a-z or 0-9 in the columns when a header is clicked

    myScrollTable = new JScrollPane(myTable); 
    myScrollTable.setSize(1282,600); 
    myScrollTable.setLocation(0,120); 
    System.out.println("Creating compare table");

    firstPanel.add(myScrollTable);
}

public static void main(String[] args)
{
    CompFrame cf = new CompFrame();
    cf.runGUI();
}
}

I would appreciate any help. Thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dan
  • 7,286
  • 6
  • 49
  • 114
  • 1
    *"I am trying to add the ability to filter.."* Where in that code have you tried to establish a `TableRowSorter`? SO is not a 'please finish my task' site. What is your *specific* question? – Andrew Thompson Mar 17 '15 at 09:54
  • My attempt has been removed from this because it completely messed up my program. My specific question is how do you add a filter correctly and simply? – Dan Mar 17 '15 at 09:57
  • 1) `firstPanel.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Possible duplicate of [Component for filtering a list](http://stackoverflow.com/q/28621617/418556) (as if the source code the OP linked to, wasn't..). – Andrew Thompson Mar 17 '15 at 09:58
  • *"My specific question is how do you add a filter correctly.."* Follow the tutorial, and if that is not simple enough for you.. *"..and simply?"* ..concede that you are not cut out for programming, and pay someone that is. – Andrew Thompson Mar 17 '15 at 10:00
  • A related example is examined [here](http://stackoverflow.com/q/17854854/230513). – trashgod Mar 17 '15 at 10:37
  • Thank you @AndrewThompson, I managed to get it working thinks to your link but out of interest is there any way for it to search the whole table and not just the first column when you are filtering? – Dan Mar 17 '15 at 10:40
  • Of course there is a way to filter all the columns! Somebody that is cut out for programming should be able to get some progress, before asking a more specific question.. ;) – Andrew Thompson Mar 17 '15 at 10:48
  • @AndrewThompson could you look at this question please http://stackoverflow.com/questions/29106565/filtering-a-jtable-in-two-columns? I attempted to get it to work but no luck – Dan Mar 17 '15 at 18:06

1 Answers1

1

It's not perfect by any means but provided you have a textfield that you can apply an action event to, you can use a table sorter. It's not the cleanest but it should work

  public void searchTable(String input) {
         final TableRowSorter<TableModel> sorter = new TableRowSorter<>(yourTable.getModel());
         allEventsTable.setRowSorter(sorter);
         if (input.length() != 0) {
                sorter.setRowFilter(RowFilter.regexFilter("(?i)" + input));

         } else {
                sorter.setRowFilter(null);

         }

}
Paulo
  • 602
  • 1
  • 8
  • 20