1 43 44 package org.jfree.ui; 45 46 import javax.swing.JTable ; 47 import javax.swing.table.JTableHeader ; 48 import javax.swing.table.TableColumnModel ; 49 50 55 public class SortableTable extends JTable { 56 57 58 private SortableTableHeaderListener headerListener; 59 60 65 public SortableTable(final SortableTableModel model) { 66 67 super(model); 68 69 final SortButtonRenderer renderer = new SortButtonRenderer(); 70 final TableColumnModel cm = getColumnModel(); 71 for (int i = 0; i < cm.getColumnCount(); i++) { 72 cm.getColumn(i).setHeaderRenderer(renderer); 73 } 74 75 final JTableHeader header = getTableHeader(); 76 this.headerListener = new SortableTableHeaderListener(model, renderer); 77 header.addMouseListener(this.headerListener); 78 header.addMouseMotionListener(this.headerListener); 79 80 model.sortByColumn(0, true); 81 82 } 83 84 91 public void setSortableModel(final SortableTableModel model) { 92 93 super.setModel(model); 94 this.headerListener.setTableModel(model); 95 final SortButtonRenderer renderer = new SortButtonRenderer(); 96 final TableColumnModel cm = getColumnModel(); 97 for (int i = 0; i < cm.getColumnCount(); i++) { 98 cm.getColumn(i).setHeaderRenderer(renderer); 99 } 100 model.sortByColumn(0, true); 101 102 } 103 104 } 105 | Popular Tags |