i'm working on a personal project with Java and Swing under Eclipse.
I've a custom JTable to render cells.
public class CustomJTable extends JTable{
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
// change background of rows if [row,13] is even
c.setBackground(getBackground());
if( (int)getModel().getValueAt(row, 13) %2 == 0)
c.setBackground(Color.YELLOW);
// change font, border e background of cells if a string field is equal to some predeterminated value
Font myFont = new Font(TOOL_TIP_TEXT_KEY, Font.ITALIC | Font.BOLD, 12);
c.setForeground(getForeground());
if (getModel().getValueAt(row, column)=="VALUE"){
((JComponent) c).setBorder(new MatteBorder(1, 1, 1, 1, Color.RED)); //needs cast for using setBorder
c.setFont(myFont);
c.setForeground(Color.RED);
c.setBackground(new Color(255, 230, 230));
}
//set disabled cells appearance
if (getModel().getValueAt(row, column)=="DISABLED"){
((JComponent) c).setBorder(new MatteBorder(1, 1, 1, 1, Color.GRAY));
c.setForeground(Color.LIGHT_GRAY);
c.setBackground(Color.LIGHT_GRAY);
}
return c;
}
My CustomJTable take values from a custom TableModel (extends AbstractTableModel) that contains a vector of class with overrided methods.
If I add an element in the vector with something like this myModel.getVector().add(element)
I have no problem.
After I type myTable.updateUI()
, Row is automatically added to CustomJtable and it's right and rendered as well. Everything is perfect!!!
The problem occure when I try to add rows from an external .XML that I saved before. Data that I added to my JTable are right, YELLOW rows background changed as well, but cells are not rendered (not cells border, not font change, not RED cells background).
I tried everything. validate()
, repaint()
, fireTableCellChanged()
...
I cannot find error. Can anyone help me?