Estou tentando mudar a cor do background de determinadas células da JTable, porém quando eu mudo a cor de uma coluna do tipo Boolean ela perde o formato de um CheckBox e aparece escrito "true" ou "false". Quero saber se é possível mudar o background das colunas Boolean sem perder o formato de CheckBox.
Código que estou usando para mudar a cor da célula:
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class ColorCellRenderer extends DefaultTableCellRenderer {
private final Color par = new Color(255, 255, 255);
private final Color impar = new Color(242, 242, 242);
private final Color color;
private final int[] rows;
public ColorCellRenderer(Color color, int[] rows) {
super();
this.color = color;
this.rows = rows;
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
if (row%2 != 0) {
this.setBackground(impar);
} else {
this.setBackground(par);
}
if(this.color != null && this.rows != null){
for (int i=0 ; i<this.rows.length ; i++) {
int rowToPaint = this.rows[i];
if(rowToPaint == row){
this.setBackground(this.color);
}
}
}else if(this.color != null){
this.setBackground( this.color );
}
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
public void validate() {
}
public void revalidate() {
}
}
Para usar a classe acima:
int[]rows = {0,1,4};
jTable1.getColumnModel().getColumn(0).setCellRenderer(new ColorCellRenderer(Color.blue,rows));
jTable1.getColumnModel().getColumn(1).setCellRenderer(new ColorCellRenderer(Color.yellow,rows));
jTable1.getColumnModel().getColumn(2).setCellRenderer(new ColorCellRenderer(Color.pink,rows));
jScrollPane1.setViewportView(jTable1);
Código de teste com tabela:
import java.awt.Color;
public class NewJFrame2 extends javax.swing.JFrame {
public NewJFrame2() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(null);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"123", "qwe", "asd", new Boolean(false)},
{"456", "ert", "dfg", new Boolean(true)},
{"879", "uio", "jkl", new Boolean(true)},
{"0-=", "p[", "ç]", new Boolean(false)},
{"zxc", "cvb", "nbm", new Boolean(true)}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
) {
Class[] types = new Class [] {
java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
int[]rows = {0,1,4};
jTable1.getColumnModel().getColumn(0).setCellRenderer(new ColorCellRenderer(Color.blue,rows));
jTable1.getColumnModel().getColumn(1).setCellRenderer(new ColorCellRenderer(Color.yellow,rows));
jTable1.getColumnModel().getColumn(2).setCellRenderer(new ColorCellRenderer(Color.pink,rows));
jScrollPane1.setViewportView(jTable1);
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(10, 11, 375, 275);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame2().setVisible(true);
}
});
}
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
}
Exemplo de como fica a JTable:
Se eu adicionar a linha jTable1.getColumnModel().getColumn(3).setCellRenderer(new ColorCellRenderer(Color.orange,rows));
a JTable aparece assim: