1
Hello, I’m working with Jtables and on such a Jtable I needed to leave some lines in differentiated colors, so I implemented the method
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 defaultRender
minhaTabela.setDefaultRenderer(Object.class,new cellRenderModel(mCard));
The problem:
before the implementation of this method, as selected by Jtable, he selected the entire line (leave the selected line in blue) as is the Jtable standard. However, after formatted as described, it lost this feature, I would like it to come back as before, that it selects the entire line as it 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 slightly more emblematic than the common selection, around such a cell, but when the table loses focus, this feature also disappears, only coming back when it regains focus again, I’d like to make that appear all the time. and if possible, change also.
NOTE: I tried to add this mouseListener but it didn’t 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 my Jtable’s "settings":
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);
From Now, thank you!
This doesn’t feel right:
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
What is the need for this?– user28595
this serves to treat the specific cell as a Jlabel, facilitating the way to leave the background different color
– Thomas Braz Pinto
To select an entire, single line, one
suaTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
– user28595
was with MULTIPLE_INTERVAL_SELECTION, switched to your tip, made no difference :/
– Thomas Braz Pinto
I put my Jtable’s "settings" in doubt, hope it helps to know what I’m doing wrong
– Thomas Braz Pinto
I recommend you change the first line of your Nderer, leave it super isolated. Do the test to see if it works again.
– user28595
I don’t know if this is what you suggested, but following "more or less" your logic of reasoning, I managed to solve, I will post the code as an answer, thanks even Diego!
– Thomas Braz Pinto