-1

I'm having some problems related to my Java project.

I am using a JTable and a JTextField used to filter the rows of the table, and here's my problem: if I write something in this textfield the table appositely filters the results, but when I delete all the text in this textfield there is one last character after the "" string that is invisible. If I use textfield.setText("") the textfield actually isn't empty at all and it doesn't allow to refresh my table with the old rows.

In other words I need to set the textfield with "" string, so it can update\refresh the JTable with the ordinary rows (without any filter applied).

Is there any method or listener that can solve this problem?

Thanks.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
andy_X91
  • 15
  • 6
  • 2
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – DavidPostill Aug 03 '14 at 19:44

1 Answers1

3

You should be using a DocumentListener which listens for changes in the underlying document of the test field.

But it sounds like your problem is that you are not doing a check before setting the filter. If there is no text, you should set the filter to null.

    if (text.trim().length() == 0) { // or if (text.isEmpty())
        rowSorter.setRowFilter(null);
    } else {
        rowSorter.setRowFilter(RowFilter.regexFilter(text));
    }

See full example here

Also see more explanation at How to Use Tables: Sorting and Filtering

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks, I solved using a Document Listener as you said. I also used Document Listener before too, but I associated it to a wrong event (KeyPressed) – andy_X91 Aug 03 '14 at 21:13