KickJava   Java API By Example, From Geeks To Geeks.

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


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  * NumberCellRenderer.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: NumberCellRenderer.java,v 1.6 2006/04/30 09:07:20 taqua Exp $
36  *
37  * Changes (from 26-Oct-2001)
38  * --------------------------
39  * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
40  * 11-Mar-2002 : Updated import statements (DG);
41  *
42  */

43
44 package org.jfree.ui;
45
46 import java.awt.Component JavaDoc;
47 import java.text.NumberFormat JavaDoc;
48
49 import javax.swing.JTable JavaDoc;
50 import javax.swing.SwingConstants JavaDoc;
51 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
52
53 /**
54  * A table cell renderer that formats numbers with right alignment in each cell.
55  *
56  * @author David Gilbert
57  */

58 public class NumberCellRenderer extends DefaultTableCellRenderer JavaDoc {
59
60     /**
61      * Default constructor - builds a renderer that right justifies the
62      * contents of a table cell.
63      */

64     public NumberCellRenderer() {
65         super();
66         setHorizontalAlignment(SwingConstants.RIGHT);
67     }
68
69     /**
70      * Returns itself as the renderer. Supports the TableCellRenderer interface.
71      *
72      * @param table the table.
73      * @param value the data to be rendered.
74      * @param isSelected a boolean that indicates whether or not the cell is
75      * selected.
76      * @param hasFocus a boolean that indicates whether or not the cell has
77      * the focus.
78      * @param row the (zero-based) row index.
79      * @param column the (zero-based) column index.
80      *
81      * @return the component that can render the contents of the cell.
82      */

83     public Component JavaDoc getTableCellRendererComponent(final JTable JavaDoc table,
84             final Object JavaDoc value, final boolean isSelected,
85             final boolean hasFocus, final int row, final int column) {
86
87         setFont(null);
88         final NumberFormat JavaDoc nf = NumberFormat.getNumberInstance();
89         if (value != null) {
90           setText(nf.format(value));
91         }
92         else {
93           setText("");
94         }
95         if (isSelected) {
96             setBackground(table.getSelectionBackground());
97         }
98         else {
99             setBackground(null);
100         }
101         return this;
102     }
103
104 }
105
Popular Tags