class cellRenderModel extends DefaultTableCellRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
ModelCartao mCard;
public cellRenderModel(ModelCartao mc) {
this.mCard = mc;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Color c = Color.WHITE;
if (mCard.isCor(row)) {//Verifica se é para pintar ou não (funcionando perfeitamente!)
c = Color.GREEN;
label.setBackground(c);
} else {
c = Color.WHITE;
label.setBackground(c);
}
return label;
}
}
And left as table's defaultRender
minhaTabela.setDefaultRenderer(Object.class,new cellRenderModel(mCard));
The problem:
Before implementing this method, as you selected JTable, it selected the entire line (leave the selected line in blue), as is the JTable pattern. However, after formatting as described, he lost that feature, would like it to go back as before, let him select the entire line as he clicks or presses up or down.
And another problem: when I click on the table, it shows a "focus" on the cell, like a blue a little more emblematic than the common selection, around such a cell, but when the table loses focus, this characteristic also disappears, coming back only when it gains focus again, I would like to make it appear always. and if possible, change as well.
NOTE: I already tried to add this mouseListener but it did not work:
minhaTabela.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
int row = minhaTabela.getSelectedRow();
int col = minhaTabela.getSelectedColumn();
tbFin.setRowSelectionInterval(row,row);
tbFin.setColumnSelectionInterval(col,col);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
Here are the "settings" of my JTable:
tb.getTableHeader().setReorderingAllowed(false);
tb.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tb.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
tb.getTableHeader().setResizingAllowed(false);
tb.setColumnSelectionAllowed(true);
tb.setRowSelectionAllowed(true);
tb.setDefaultRenderer(Object.class, new celRenderModel(mCard));
tb.setCellSelectionEnabled(false);
tb.setRowSelectionAllowed(true);
Now, thank you!