Question

I'm somewhat new to Java and I'm having problems with JTable with sort and TableCellRenderer.

I have a table with 15 columns populated with values where on some columns I use the TableCellRenderer to change the color of foreground to green or red acording the values on the cell.

Everything is working perfectly until I try to sort by some column (the sorting part is ok)... The problem is that the formating colors don't reflect the change made by the sort operation. The colors stay unchanged at their original position on the table before the sort. Is there a simple way to overcome this problem?

I searched, but I did not find a solution I can implement.

TIA

JL

EDIT: Related source code posted (not complete implementation)

private void buildTab(){
    myIUserInterface.removeBottomTab("just a test");
    myJPanel = myIUserInterface.getBottomTab("just a test");
    myJScollPane = new JScrollPane();
    myTable = new JTable();
    myTable.setAutoCreateRowSorter(true);
    myTableContent = myFillTableData();
    String[] tableHeaders = {
            "X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "X11", "X12", "X13", "X14", "X15"
        };
    myTable.setModel(new DefaultTableModel(myTableContent, tableHeaders) {

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }
    });
    MyChangeCellColor myCCC = new MyChangeCellColor();
    Enumeration<TableColumn> allColumns = myTable.getColumnModel().getColumns();
    while(allColumns.hasMoreElements()){
        TableColumn column = allColumns.nextElement();
        column.setCellRenderer(myCCC);
    }
    myJScollPane.setViewportView(myTable);

    GroupLayout myLayout = new GroupLayout(myJPanel);
    myJPanel.setLayout(myLayout);
    myLayout.setHorizontalGroup(
        myLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(myJScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 890, Short.MAX_VALUE)
    );
    myLayout.setVerticalGroup(
        myLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(myJScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 672, Short.MAX_VALUE)
    );
}


class MyChangeCellColor extends JLabel implements TableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {
        setHorizontalAlignment(CENTER);
        setOpaque(true);
        setBackground(Color.WHITE);
        if (column == 0){
            setForeground(Color.BLACK);
        }
        if (column == 1){
            setForeground(Color.BLACK);
        }
        if (column == 2){
            setForeground(Color.BLACK);
        }
        if (column == 3){
            setForeground(Color.BLACK);
        }
        if (column == 4){
            if ("LONG".equals(myTable.getModel().getValueAt(row, 4))) {
                setForeground(Color.GREEN.darker());
            }
            if ("SHORT".equals(myTable.getModel().getValueAt(row, 4))) {
                setForeground(Color.RED);
            }
        }
        if (column == 5){
            setForeground(Color.BLACK);
        }
        if (column == 6){
            setForeground(Color.BLACK);
        }
        if (column == 7){
            setForeground(Color.BLACK);
        }
        if (column == 8){
            setForeground(Color.BLACK);
        }
        if (column == 9){
            setForeground(Color.BLACK);
        }
        if (column == 10){
            setForeground(Color.BLACK);
        }
        if (column == 11){
            double d = Double.valueOf(myTable.getModel().getValueAt(row, 11).toString());
            if (d > 0) {
                setForeground(Color.GREEN.darker());
            }
            if (d == 0) {
                setForeground(Color.BLACK);
            }
            if (d < 0) {
                setForeground(Color.RED);
            }
        }
        if (column == 12){
            double d = Double.valueOf(myTable.getModel().getValueAt(row, 12).toString());
            if (d > 0) {
                setForeground(Color.GREEN.darker());
            }
            if (d == 0) {
                setForeground(Color.BLACK);
            }
            if (d < 0) {
                setForeground(Color.RED);
            }
        }
        if (column == 13){
            setForeground(Color.BLACK);
        }
        if (column == 14){
            setForeground(Color.BLACK);
        }
        setText(value.toString());
        return this;
    }
}

EDIT 2: Solution fond and pasted for reference if anyone needs it! Just need to reference the model rows on the rows returned by JTable on TableCellRenderer class (not sure if it's correctly explained anyway you can see the changes below):

class MyChangeCellColor extends JLabel implements TableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {
        setHorizontalAlignment(CENTER);
        setOpaque(true);
        setBackground(Color.WHITE);
        if (column == 0){
            setForeground(Color.BLACK);
        }
        if (column == 1){
            setForeground(Color.BLACK);
        }
        if (column == 2){
            setForeground(Color.BLACK);
        }
        if (column == 3){
            setForeground(Color.BLACK);
        }
        if (column == 4){
            if ("LONG".equals(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 4))) {
                setForeground(Color.GREEN.darker());
            }
            if ("SHORT".equals(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 4))) {
                setForeground(Color.RED);
            }
        }
        if (column == 5){
            setForeground(Color.BLACK);
        }
        if (column == 6){
            setForeground(Color.BLACK);
        }
        if (column == 7){
            setForeground(Color.BLACK);
        }
        if (column == 8){
            setForeground(Color.BLACK);
        }
        if (column == 9){
            setForeground(Color.BLACK);
        }
        if (column == 10){
            setForeground(Color.BLACK);
        }
        if (column == 11){
            double d = Double.valueOf(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 11).toString());
            if (d > 0) {
                setForeground(Color.GREEN.darker());
            }
            if (d == 0) {
                setForeground(Color.BLACK);
            }
            if (d < 0) {
                setForeground(Color.RED);
            }
        }
        if (column == 12){
            double d = Double.valueOf(myTable.getModel().getValueAt(myTable.convertRowIndexToModel(row), 12).toString());
            if (d > 0) {
                setForeground(Color.GREEN.darker());
            }
            if (d == 0) {
                setForeground(Color.BLACK);
            }
            if (d < 0) {
                setForeground(Color.RED);
            }
        }
        if (column == 13){
            setForeground(Color.BLACK);
        }
        if (column == 14){
            setForeground(Color.BLACK);
        }
        setText(value.toString());
        return this;
    }
}
Was it helpful?

Solution

The problem is that the formating colors don't reflect the change made by the sort operation.

You have a problem with the logic in your renderer. You are probably accessing data in the table model with the "view row" instead of the "model row".

Since you didn't post your SSCCE demonstrating the problem I can't give you a specific solution only a general solution. Look at the following JTable methods:

convertRowIndexToModel(int viewRowIndex) 
convertRowIndexToView(int modelRowIndex)  

The renderer receives the view row number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top