Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Icon needed in JTableHeader for Sorting

 
Rohit Kedia
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I needed to sort my JTable selective columns based on the columns which I click.
I was able to successfully implement that using this link form java2s site Java2s

Now i needed is that whenever I sort the Table by clicking on any of the TableHeader an Icon should show in the Header like arrow up or down for ascending or descending order.

I also have a problem here which is I am using the column Names as <HTML><B>column Name</B></HTML> as I have to show some column Names in bold.

Please help me
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throw away all you've done and start using TableRowSorter. Just instantiate one, call table.setRowSorter and you'll get all of that for free!
Disclaimer: available since Java 6
 
Rohit Kedia
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Throw away all you've done and start using TableRowSorter. Just instantiate one, call table.setRowSorter and you'll get all of that for free!
Disclaimer: available since Java 6


Sir I wanted to know that can I sort some specific columns and not all the columns. I had used Rowsorter but the Issue was that
for Example :
Suppose I have 12 columns in my JTable I want to sort the Row data based on only 6 columns can this be done using Rowsorter.
 
Rob Camick
Ranch Hand
Posts: 2600
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to sort the Row data based on only 6 columns can this be done using Rowsorter.


A JTable actually uses the DefaultRowSorter and yes this can be done using this class.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does nobody look into the Swing tutorials and/or the API docs anymore?
-> http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting
-> http://java.sun.com/javase/7/docs/api/javax/swing/DefaultRowSorter.html#setSortable(int, boolean)
 
Rohit Kedia
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting this Error





 
Peter Taucher
Ranch Hand
Posts: 174
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the examples? They all have something similar, that is missing in your code. Exactly. They use the TableRowSorter constructor providing an actual model (e.g. table.getModel()). I think that might be the problem here.
 
Rohit Kedia
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that Issue is resolved but the problem still prevails which is it still sorting all the Columns I only want to sort on basis of certain specific columns.


there are 12 columns but I want to use only these column for sorting apart from that I was adding 10 blank rows at the end I do not want to include them in sorting but it is still coming.

 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You did see the second link I provided (the link to the setSortable() method)? You can explicitely define which columns should be sortable (and which not). Please show some effort and at least pretend to read the provided docs/tutorials...
 
Rohit Kedia
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Taucher wrote:You did see the second link I provided (the link to the setSortable() method)? You can explicitely define which columns should be sortable (and which not). Please show some effort and at least pretend to read the provided docs/tutorials...


I am really very sorry for this... actually I was bit tensed... so skipped it...

anyways the Issue is resolved thanks for the Effort... and help...
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also having similar issue,
I want sorting to be applied only to some selected column, but Rohit has not posted the way he has solved his issue, At least he should have realized to post the answer so that other can get benefit of it.

I am still trying to find the answer of this problem,
Any further help is appreciated.
 
David Byron
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh k Jha wrote:I am also having similar issue,
I want sorting to be applied only to some selected column... I am still trying to find the answer of this problem...

Have you looked here?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic