KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > contrib > javapro > SortHeaderRenderer


1 /*
2 =====================================================================
3
4   SortHeaderRenderer.java
5   
6   Created by Claude Duguay
7   Copyright (c) 2002
8   
9 =====================================================================
10 */

11
12 package contrib.javapro;
13
14 import java.awt.*;
15 import javax.swing.*;
16 import javax.swing.table.*;
17
18 public class SortHeaderRenderer
19   extends DefaultTableCellRenderer
20 {
21   public static Icon NONSORTED =
22     new SortArrowIcon(SortArrowIcon.NONE);
23   public static Icon ASCENDING =
24     new SortArrowIcon(SortArrowIcon.ASCENDING);
25   public static Icon DECENDING =
26     new SortArrowIcon(SortArrowIcon.DECENDING);
27   
28   public SortHeaderRenderer()
29   {
30     setHorizontalTextPosition(LEFT);
31     setHorizontalAlignment(CENTER);
32   }
33   
34   public Component getTableCellRendererComponent(
35     JTable table, Object JavaDoc value, boolean isSelected,
36     boolean hasFocus, int row, int col)
37   {
38     int index = -1;
39     boolean ascending = true;
40     if (table instanceof JSortTable)
41     {
42       JSortTable sortTable = (JSortTable)table;
43       index = sortTable.getSortedColumnIndex();
44       ascending = sortTable.isSortedColumnAscending();
45     }
46     if (table != null)
47     {
48       JTableHeader header = table.getTableHeader();
49       if (header != null)
50       {
51         setForeground(header.getForeground());
52         setBackground(header.getBackground());
53         setFont(header.getFont());
54       }
55     }
56     Icon icon = ascending ? ASCENDING : DECENDING;
57     setIcon(col == index ? icon : NONSORTED);
58     setText((value == null) ? "" : value.toString());
59     setBorder(UIManager.getBorder("TableHeader.cellBorder"));
60     return this;
61   }
62 }
63
64
Popular Tags