Hi. I found the following code off of Sun's website to sort columns in a JTable by clicking the column header. The only thing that this code does not do, is sort descending. If I click the column to sort it sorts ascending, if I click the same column again, I want it to sort descending. Could someone please help me modify this code to accomplish this? I should probably also admit, that I am desperatly trying to figure out exactly how this code works,(not that good yet) so that is why I am asking for help to modify it. Thanks for any help. It is much appreciated!
it seems to me this ascending boolean represents ascending or descending, true or false. and it appears that the action performed always sends true. It also looks that they way to get it to display descending without any changes you just hold the shift key down when you click on the column. I know this from these lines.
What you could always do to just make it click and change the order it is in, is by having the desclaration from above in the actionPerformed boolean ascending = (shiftPressed == 0); move the ascending variable to be an instance variable then call... sorter.sortByColumn(column, !ascending); this will pass the opposite of ascending to the sortByColumn which will change the current sort. Make sure you start ascending to be false, so that the first call will sort the column Ascending, and the next click should be descending. I hope that works Mark [ May 24, 2002: Message edited by: Mark Spritzler ] [ May 24, 2002: Message edited by: Mark Spritzler ]
OK I got one thing worng in that post. SOrry, but after I left work I realized that you will still always get ascending. after making the boolean ascending an instance variable. Ok currently you have this code.
change it to
this takes out the use of the Shift key, which you didn't want anyway, and the instance variable ascending gets changed just before the sortByColumn call, so it changes it to the opposite of what it is. So for instance you start of with False when the instance is created. That's the default I said above to have, then the user clicks the column, it calls the mouseClicked method, which will change ascending to true, and call sortByColumn effectively sorting it ascending. The next time the user clicks the Column, it will change ascending to equal false, and pass that to sortByColumn, effectively sorting it descending. Whew. Finally got that written out, and now it won't bother me any more this weekend. Mark
Joined: Feb 28, 2001
Mark, Thanks so much for the help! It worked like a charm. My only issue is I'm afraid I don't understand what happened here. First off, what exactly does this line of code do:
What is the ! doing? I don't believe I've run across this before. Thanks again!
Basically you are turning a light on and off. The ! is the "Not" so if ascending = true then !ascending = false. So in the assignment of ascending = !ascending you are just changing for true to false and back and forth, like constantly changing the light switch. so ascending = true then ascending = !ascending now makes ascending false, then the next time ascending = false then ascending = !ascending now makes ascending true. Mark
Joined: Feb 28, 2001
Thanks for clearing that up for me! Have a great Day!!