KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > ui > DateCellRenderer


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  * DateCellRenderer.java
29  * ---------------------
30  * (C) Copyright 2003, 2004, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: DateCellRenderer.java,v 1.6 2006/04/30 09:07:20 taqua Exp $
36  *
37  * Changes (from 26-Oct-2001)
38  * --------------------------
39  * 24-Jul-2003 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.ui;
44
45 import java.awt.Component JavaDoc;
46 import java.text.DateFormat JavaDoc;
47
48 import javax.swing.JTable JavaDoc;
49 import javax.swing.SwingConstants JavaDoc;
50 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
51
52 /**
53  * A table cell renderer that formats dates.
54  *
55  * @author David Gilbert
56  */

57 public class DateCellRenderer extends DefaultTableCellRenderer JavaDoc {
58
59     /** The formatter. */
60     private DateFormat JavaDoc formatter;
61     
62     /**
63      * Default constructor.
64      */

65     public DateCellRenderer() {
66         this(DateFormat.getDateTimeInstance());
67     }
68     
69     /**
70      * Creates a new renderer.
71      *
72      * @param formatter the formatter.
73      */

74     public DateCellRenderer(final DateFormat JavaDoc formatter) {
75         super();
76         this.formatter = formatter;
77         setHorizontalAlignment(SwingConstants.CENTER);
78     }
79
80     /**
81      * Returns itself as the renderer. Supports the TableCellRenderer interface.
82      *
83      * @param table the table.
84      * @param value the data to be rendered.
85      * @param isSelected a boolean that indicates whether or not the cell is
86      * selected.
87      * @param hasFocus a boolean that indicates whether or not the cell has
88      * the focus.
89      * @param row the (zero-based) row index.
90      * @param column the (zero-based) column index.
91      *
92      * @return the component that can render the contents of the cell.
93      */

94     public Component JavaDoc getTableCellRendererComponent(final JTable JavaDoc table,
95             final Object JavaDoc value, final boolean isSelected,
96             final boolean hasFocus, final int row, final int column) {
97
98         setFont(null);
99         if (value != null) {
100           setText(this.formatter.format(value));
101         }
102         else {
103           setText("");
104         }
105         if (isSelected) {
106             setBackground(table.getSelectionBackground());
107         }
108         else {
109             setBackground(null);
110         }
111         return this;
112     }
113
114 }
115
Popular Tags