Correct one cell in JTable instead of whole row

I'm having a little problem and I'm struggling to solve it. Basically what happens is I have a JTable that is populated with an array that I get from an API call.
What I currently have is that if the device shows up as online it will change to GREEN, if disabled then light gray.
The problem is that this affects the entire ROW, not just the CELL status. I want the status cell to highlight green. Any help could be helpful.

custTable = new javax.swing.JTable(model){

@Override
public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
    int columnIndex) {
    JComponent component = (JComponent) super.prepareRenderer(renderer, rowIndex, columnIndex);

    if(getValueAt(rowIndex,1).toString().equalsIgnoreCase("Online"))
    {
        component.setBackground(Color.GREEN);
    }
    else if(getValueAt(rowIndex,1).toString().equalsIgnoreCase("Offline"))
    {
        component.setBackground(Color.lightGray);
    }

    return component;
}

      

Screenshot

+3


source to share


2 answers


Do not override the prepareRenderer () method. Typically, you only override this method if you want rendering to be efficient for the input line. This approach is useful because the rendering code is in one place and you don't need to create your own renderers for each column in the table.

However, for a specific rendering of a cell in a specific column, you must create a custom renderer for the column.

Read the section from the Swing tutorial on Using Custom Renders for more information and examples.



The tutorial example implements the TableCellRenderer interface. It might be easier to extend the default renderer:

class ColorRenderer extends DefaultTableCellRenderer
{
    @Override
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        if (!isSelected)
        {
            int viewColumn = convertColumnIndexToView(1)
            String value = getModel().getValueAt(rowIndex, viewColumn).toString();

            if ("Online".equalsIgnoreCase(value))
                setBackground( Color.GREEN );
            else
                setBackground( Color.lightgray );

            return this;
        }
    }
}

      

Note that you must convert the column index in case the user has changed the order of the columns in the table.

+4


source


I agree with camickr, but you can also fix your code like this to avoid problems.



@Override
public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
    int columnIndex) {
    JComponent component = (JComponent) super.prepareRenderer(renderer, rowIndex, columnIndex);

    if(getValueAt(rowIndex,1).toString().equalsIgnoreCase("Online") && columnIndex == 1)
    {
        component.setBackground(Color.GREEN);
    }
    else if(getValueAt(rowIndex,1).toString().equalsIgnoreCase("Offline") && columnIndex == 1)
    {
        component.setBackground(Color.lightGray);
    } else {
        component.setBackground(isRowSelected(rowIndex)? getSelectedBackground() : getBackground());
    }

    return component;
}

      

+2


source







All Articles