Frames | No Frames |
1: /* ======================================================================== 2: * JCommon : a free general purpose class library for the Java(tm) platform 3: * ======================================================================== 4: * 5: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jcommon/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ------------------ 28: * SortableTable.java 29: * ------------------ 30: * (C) Copyright 2000-2004, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * $Id: SortableTable.java,v 1.5 2005/11/16 15:58:41 taqua Exp $ 36: * 37: * Changes (from 26-Oct-2001) 38: * -------------------------- 39: * 26-Oct-2001 : Changed package to com.jrefinery.ui.*; 40: * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG); 41: * 42: */ 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: /** 51: * A simple extension of JTable that supports the use of a SortableTableModel. 52: * 53: * @author David Gilbert 54: */ 55: public class SortableTable extends JTable { 56: 57: /** A listener for sorting. */ 58: private SortableTableHeaderListener headerListener; 59: 60: /** 61: * Standard constructor - builds a table for the specified model. 62: * 63: * @param model the data. 64: */ 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: /** 85: * Changes the model for the table. Takes care of updating the header listener at the 86: * same time. 87: * 88: * @param model the table model. 89: * 90: */ 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: }