DateCellRenderer.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.7 2007/11/02 17:50:36 taqua Exp $
  36. *
  37. * Changes (from 26-Oct-2001)
  38. * --------------------------
  39. * 24-Jul-2003 : Version 1 (DG);
  40. *
  41. */
  42. package org.jfree.ui;
  43. import java.awt.Component;
  44. import java.text.DateFormat;
  45. import javax.swing.JTable;
  46. import javax.swing.SwingConstants;
  47. import javax.swing.table.DefaultTableCellRenderer;
  48. /**
  49. * A table cell renderer that formats dates.
  50. *
  51. * @author David Gilbert
  52. */
  53. public class DateCellRenderer extends DefaultTableCellRenderer {
  54. /** The formatter. */
  55. private DateFormat formatter;
  56. /**
  57. * Default constructor.
  58. */
  59. public DateCellRenderer() {
  60. this(DateFormat.getDateTimeInstance());
  61. }
  62. /**
  63. * Creates a new renderer.
  64. *
  65. * @param formatter the formatter.
  66. */
  67. public DateCellRenderer(final DateFormat formatter) {
  68. super();
  69. this.formatter = formatter;
  70. setHorizontalAlignment(SwingConstants.CENTER);
  71. }
  72. /**
  73. * Returns itself as the renderer. Supports the TableCellRenderer interface.
  74. *
  75. * @param table the table.
  76. * @param value the data to be rendered.
  77. * @param isSelected a boolean that indicates whether or not the cell is
  78. * selected.
  79. * @param hasFocus a boolean that indicates whether or not the cell has
  80. * the focus.
  81. * @param row the (zero-based) row index.
  82. * @param column the (zero-based) column index.
  83. *
  84. * @return the component that can render the contents of the cell.
  85. */
  86. public Component getTableCellRendererComponent(final JTable table,
  87. final Object value, final boolean isSelected,
  88. final boolean hasFocus, final int row, final int column) {
  89. setFont(null);
  90. if (value != null) {
  91. setText(this.formatter.format(value));
  92. }
  93. else {
  94. setText("");
  95. }
  96. if (isSelected) {
  97. setBackground(table.getSelectionBackground());
  98. }
  99. else {
  100. setBackground(null);
  101. }
  102. return this;
  103. }
  104. }