• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable question....again!

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

then later in compare method


Then near the end I see

and finally in the action performed I see

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 ]
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clearing that up for me!
Have a great Day!!
reply
    Bookmark Topic Watch Topic
  • New Topic