Ever wanted to change the color of a JCheckBox in a JTable to the table's foreground color? Now you can!
Example Screenshot:

This is a renderer for Booleans in a JTable.
JAVA:
-
import java.awt.Color;
-
import java.awt.Component;
-
import java.awt.Graphics;
-
-
import javax.swing.JCheckBox;
-
import javax.swing.JTable;
-
import javax.swing.table.TableCellRenderer;
-
-
/**
-
* This class implements a boolean renderer for a JTable. It overrides the icon for the CheckBox to draw in the
-
* foreground color.
-
*/
-
public BooleanRenderer() {
-
super();
-
setIcon(new ColorableMetalCheckBoxIcon());
-
}
-
-
boolean isSelected, boolean hasFocus, int row, int column) {
-
if (isSelected) {
-
setForeground(table.getSelectionForeground());
-
super.setBackground(table.getSelectionBackground());
-
} else {
-
setForeground(table.getForeground());
-
setBackground(table.getBackground());
-
}
-
return this;
-
}
-
-
/**
-
* Changes the check box check mark to be the foreground color.<br/>
-
* <b>NOTE:</b>This was found here http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4449413 .
-
*/
-
g.setColor(c.getForeground());
-
super.drawCheck(c, g, x, y);
-
g.setColor(old);
-
}
-
}
-
}
This is the corresponding Editor.
JAVA:
-
import java.awt.Component;
-
-
import javax.swing.DefaultCellEditor;
-
import javax.swing.JCheckBox;
-
import javax.swing.JTable;
-
-
/**
-
* This class implements a boolean editor for a JTable. It overrides the icon for the CheckBox to draw in the
-
* foreground color.
-
*/
-
-
public BooleanEditor() {
-
c.setIcon(new BooleanRenderer.ColorableMetalCheckBoxIcon());
-
}
-
-
Object value,
-
boolean isSelected,
-
int row,
-
int column) {
-
if (isSelected) {
-
c.setForeground(table.getSelectionForeground());
-
c.setBackground(table.getSelectionBackground());
-
} else {
-
c.setForeground(table.getForeground());
-
c.setBackground(table.getBackground());
-
}
-
return c;
-
}
-
}
Quick and Dirty Tester Program!
JAVA:
-
import java.awt.Color;
-
import java.awt.Dimension;
-
import java.awt.FlowLayout;
-
-
import javax.swing.*;
-
import javax.swing.table.AbstractTableModel;
-
import javax.swing.table.DefaultTableModel;
-
-
/**
-
* shows how to use the BooleanEditor and the BooleanRenderer.
-
*/
-
public JTableBooleanTester(){
-
-
-
-
TestTableModel ttm = new TestTableModel();
-
-
sp1.setPreferredSize(dim);
-
-
sp2.setPreferredSize(dim);
-
-
getContentPane().add(sp1);
-
getContentPane().add(sp2);
-
-
pack();
-
}
-
-
public void run() {
-
frame.setVisible(true);
-
}
-
-
});
-
}
-
-
-
public int getRowCount() {
-
return data.length;
-
}
-
-
public int getColumnCount() {
-
-
return names.length;
-
}
-
-
return data[rowIndex][columnIndex];
-
}
-
-
switch(columnIndex){
-
case 0:
-
data[rowIndex][columnIndex] = aValue;
-
}
-
fireTableDataChanged();
-
}
-
-
public boolean isCellEditable(int rowIndex, int columnIndex) {
-
return 0 == columnIndex;
-
}
-
-
public Class getColumnClass(int columnIndex){
-
return data[0][columnIndex].getClass();
-
}
-
}
-
}
Comments
Leave a comment Trackback