简体   繁体   中英

“prepareRenderer” doesn't work when I put data from xml into my customJTable

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?

getModel().getValueAt(row, column)=="VALUE" >> that's more than likely an error already. If you want to compare strings, you need to use Object.equals to compare those. Like this: "VALUE".equals(getModel().getValueAt(row, column).toString()) . You make the same mistake a bit further comparing to the string "DISABLED" .

The second mistake is that you are using view indexes to index in the model. The row and column parameters passed in the JTable.prepareRenderer method are view indexes. You can't use those to index the model as you are doing in getModel().getValueAt(row, column) . You need to translate those indexes using JTable.convertRowIndexToModel and JTable.convertColumnIndexToModel before calling getModel().getValueAt() . You can read more about this in the introductory description in the JTable documentation .

Alternatively, use JTable.getValueAt() instead. Here you can use the view indexes passed to JTable.prepareRenderer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM