/* * [RainbowRenderer.java] * * Summary: Lets you render table data choosing the colours. * * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 3.8 2009-03-31 change order of fields in defaults.csv. */ package com.mindprod.vercheck; import javax.swing.JLabel; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableCellRenderer; import java.awt.Color; import java.awt.Component; import java.awt.Font; /** * Lets you render table data choosing the colours. * * @author Roedy Green, Canadian Mind Products * @version 3.8 2009-03-31 change order of fields in defaults.csv. * @since 2009 */ final class RainbowRenderer extends DefaultTableCellRenderer implements TableCellRenderer { private final Color foreground; private final Font font; private final int horizontalAlignment; /** * constructor * * @param font for to render the column * @param foreground foreground colour * @param horizontalAlignment e.g. JLabel.CENTER */ @SuppressWarnings( { "SameParameterValue" } ) public RainbowRenderer( Font font, Color foreground, int horizontalAlignment ) { this.foreground = foreground; this.font = font; this.horizontalAlignment = horizontalAlignment; } public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) { JLabel template = ( JLabel ) super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column ); template.setFont( font ); template.setForeground( foreground ); // we don't handle setting selected background here. // We don't get called when selection changes. // leave it up to JTable to set the background to selected or normal. template.setHorizontalAlignment( horizontalAlignment ); if ( value != null ) { template.setText( value.toString() ); } else { template.setText( null ); } return template; } }