0

In my java desktop application there is a JTable(table from swing) in that table there names of various persons. Now i want at as i write something in Search(text box for searching a name ) then after entering each letter the results of JTable are sorted .... For Example there are five Strings starting with alphabet "a" so as i enter a all those five strings must be displayed in JTable then there are 2 strings starting with "ab" as i write "b" after "a" then those 2 strings must be displayed....

..please Help me out friends

2 Answers2

0
public String[] sortedResults(String terms, String[] possibleResults){ 
    ArrayList<String> sortedResults = new ArrayList<String, String>();

    for(String currentTerm : possibleResults){
        if(currentTerm.startsWith(terms)){
            sortedResults.add(currentTerm);
        }
    }

    return sortedResults;
}
john01dav
  • 95
  • 1
  • 4
  • Sorry about that missing code. I accidently pressed enter twice when entering it I have edited my answer so that it has the full code. – john01dav Oct 19 '13 at 21:32
  • In scenarios where he's got thousands of strings to match, this is a very slow implementation. – Birb Oct 19 '13 at 21:34
  • Perhaps adding an array that is the same length as the possible results them compressing it down may work? Also, in his question he does not say that he has thousands of strings. – john01dav Oct 19 '13 at 21:36
  • Nope, that is true :) – Birb Oct 19 '13 at 21:36
  • [Profile](http://stackoverflow.com/q/2064427/230513) to see if this [example](http://stackoverflow.com/a/7605780/230513) will scale to your use case as a `CellEditor`. – trashgod Oct 19 '13 at 22:19
0

If I understand your question correctly, the RowFilter class should do exactly what you need. Note that this will only work if you first call setAutoCreateRowSorter(true) on your JTable:

private void updateFilter() {
    final String prefix = textField.getText().toLowerCase();

    ((TableRowSorter<?>) table.getRowSorter()).setRowFilter(
        new RowFilter<Object, Integer>() {
            public boolean include(Entry<?, ? extends Integer> entry) {
                String name = entry.getStringValue(COLUMN_LASTNAME);
                return name.toLowerCase().startsWith(prefix);
            }
        });
}

You'll want to call the method from a DocumentListener:

searchTextField.getDocument().addDocumentListener(new DocumentListener() {
    public void changedUpdate(DocumentEvent event) {
        updateFilter();
    }

    public void insertUpdate(DocumentEvent event) {
        updateFilter();
    }

    public void removeUpdate(DocumentEvent event) {
        updateFilter();
    }
});
VGR
  • 40,506
  • 4
  • 48
  • 63
  • 2
    Right idea but a better answer would be to point to the Swing tutorial on [Sorting and Filtering](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting) for a complete working example. Not only that the tutorial can now be used as a reference for future Swing problems. – camickr Oct 20 '13 at 03:35