• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting and Filtering a ResultSet  RSS feed

 
Pete Yanchek
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a GUI for storing contact information (first name, last name, email, phone, address, city, state, zip, skype).
The results are stored in contact table within the Derby database.

The contact list can be sorted (by any field either A-Z or Z-A).
It can also be filtered (based on whether a field starts with, ends with, or contains whatever text is entered into the filter textfield).
Both the sort button and the filter button work and work fine... but... I can't get them to work together....
That is to say that if the list is sorted, when I click the filter button, only the filtered results are displayed and the sorting is undone.
Likewise, if the list is filtered, the sort button undoes all of the filtering and only displays the sorted list.

The following method is initially called within the constructor:



Here is what my filterButtonActionPerformed method looks like:


Finally, my sortButtonActionPerformed method is this:



Does anyone see where I am going wrong here?
Why does my ResultSet automatically undo its filtering or sorting when I try to add an additional sort or filter?



 
Paul Clapham
Sheriff
Posts: 22099
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you click on a Sort button, your code starts by reading the whole table in a certain order. This happens regardless of what buttons the user clicked before.

So it's not like the second ResultSet "undoes" the first one -- you just create a new ResultSet and discard the earlier one.

You didn't say how you were displaying the data, but if you're using a JTable could I suggest you just use Swing's built-in features for sorting and filtering the contents of a JTable? The tutorial is here.
 
Pete Yanchek
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but it's not a JTable. (We aren't supposed to use one for this particular exercise).
I am just using a table within the Derby database.
 
Mark Spencers
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to do this in database. When you create query use for sorting and in specify only columns you need.
 
Knute Snortum
Sheriff
Posts: 3436
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will have to build your SQL string piece by piece.  I don't see how you could test for all possible combinations of filtering and sorting.  The pseudocode might be something like this:

Another thing to think about: what if you are filtering by many things?  Or multiple sort orders?
 
Pete Yanchek
Greenhorn
Posts: 24
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Knute. I ended up making a sort checkbox and a filter checkbox, which allows me to implement the idea that you suggested.
I should have just done it that way from the beginning. It works well now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!