tags:

views:

231

answers:

4

I am writting a customized component which need to use the little triangle icon of sortable JTable header. Maybe it's not really an icon file but a graphics painted by some class.

+1  A: 

Check this tutorial How to Use Tables

List <RowSorter.SortKey> sortKeys 
    = new ArrayList<RowSorter.SortKey>();
sortKeys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));
sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
sorter.setSortKeys(sortKeys); 

SortOrder has the following enums:

  • ASCENDING Enumeration value indicating the items are sorted in increasing order.
  • DESCENDING Enumeration value indicating the items are sorted in decreasing order.
  • UNSORTED Enumeration value indicating the items are unordered.

To customize the Icon rendering you could use check this example

stacker
I don't think this answer matches the question.
ammoQ
+3  A: 

Paiting those would be the job of a subclass of TableHeaderUI specific to the current Look-and-Feel. One L&F might load it from a file while another paints it as a vector graphic.

Michael Borgwardt
+4  A: 

I believe the sort icon comes from the look and feel. For example, in the Windows look and feel, the WindowsTableHeaderUI looks up the following to draw the appropriate icon. For ascending:

UIManager.getIcon("Table.ascendingSortIcon");

For descending:

UIManager.getIcon("Table.descendingSortIcon");

While the BasicLookAndFeel registers a value for these properties (so there is likely to be something there if you use a similar call), as @Michael Borgwardt notes (by drawing a conclusion from his answer), there is no guarantee of this since any given look and feel might choose to forego the UIManager and draw their own graphics.

Still, it might be worthwhile trying it to see if it does what you want.

Ash
solotim
+2  A: 

You have to add a row sorter to your JTable:

RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
table.setRowSorter(sorter);
Martijn Courteaux