algorithms-for-computing-li.../src/main/java/View/custom/ColorColumnRenderer.java

38 lines
1.1 KiB
Java

package View.custom;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
/**
* Source: <url> http://esus.com/creating-a-jtable-with-a-different-background-color-per-column/ </url>
*
* Applied background and foreground color to single column of a JTable
* in order to distinguish it apart from other columns.
*/
public class ColorColumnRenderer extends DefaultTableCellRenderer {
Color bkgndColor, fgndColor;
public ColorColumnRenderer(Color bkgnd, Color foregnd) {
super();
bkgndColor = bkgnd;
fgndColor = foregnd;
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
cell.setBackground(bkgndColor);
cell.setForeground(fgndColor);
cell.setFont(new Font("SansSerif", Font.BOLD, 12));
this.setHorizontalAlignment(JLabel.CENTER);
return cell;
}
}