Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Transparent Checkbox in JTable

843805May 27 2006
I am having problem in making my checkbox columns to transparent. I used AbstractTableModel to make the checkboxes:

class DownloadsTableModel extends AbstractTableModel
implements Observer
{
private final String[] columnNames = {"Document Code", "Boolean"};

private final Class[] columnClasses = {String.class, java.lang.Boolean.class};

// Get a column's class
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
......


And then I used a renderer to try to make it trasparent...

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.table.DefaultTableCellRenderer;

public class CheckboxRenderer extends JCheckBox
implements TableCellRenderer
{
boolean isSelected = false;
Color selectionColor;

public CheckboxRenderer(JTable table) {
// we'll use a translucent version of the table's default
// selection color to paint selections
Color oldCol = table.getSelectionBackground();
selectionColor = new Color(oldCol.getRed(), oldCol.getGreen(), oldCol.getBlue(), 128);

// need to be non-opaque since we'll be translucent
setOpaque(false);
}

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
//setValue((boolean) value);

// save the selected state since we'll need it when painting
//this.isSelected = isSelected;
setSelected(((Boolean) value).booleanValue());

return this;
}

// since DefaultTableCellRenderer is really just a JLabel, we can override
// paintComponent to paint the translucent selection when necessary
public void paintComponent(Graphics g) {
if (isSelected) {
g.setColor(selectionColor);
g.fillRect(0, 0, getWidth(), getHeight());
}
super.paintComponent(g);
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 24 2006
Added on May 27 2006
0 comments
186 views