Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:41:42+00:00 2026-06-17T11:41:42+00:00

I would like to set the color of a cell based on the value

  • 0

I would like to set the color of a cell based on the value of the cell. Having googled around for a bit i found out that i can do it using something like this:

public class TableCellRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(
       JTable table, Object value, 
       boolean isSelected, boolean hasFocus, 
       int row, int col)  
    {
       // get the DefaultCellRenderer to give you the basic component
       Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
       // apply your rules
       if (value.toString().equals("Red"))
          c.setBackground(Color.RED);
       else  
          c.setBackground(Color.GRAY);
       return c;
    }
 }

The problem i have though is that the code i would like to modify is already setting the TableCellRendererer for the columns of the JTable. There is a function in the code that looks like this:

private void configureTableColumns() {      
        Enumeration columns = this.table.getColumnModel().getColumns();

        while (columns.hasMoreElements()) {
            TableColumn tableColumn = (TableColumn) columns.nextElement();

            this.setCellRenderer(tableColumn);
            TableCellRenderer renderer = tableColumn.getCellRenderer();

            if (renderer == null) {
                renderer = this.table.getDefaultRenderer(Object.class);
            }
            tableColumn.setCellRenderer(renderer);
            this.setCellEditor(tableColumn);
        }
    }

With the above code, do i still need to add the TableCellRenderer class shown previously? All i want to do is to check if the value of the cell is ‘ABC’ and set the background to RED.

Update:

I did try adding my version of the TableCellRenderer as an inner class in the code i want to modify but i get an error that there is a type mismatch at tableColumn.getCellRenderer().

Type mismatch: cannot convert from TableCellRenderer to MyTableExample.TableCellRenderer

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:41:43+00:00Added an answer on June 17, 2026 at 11:41 am

    Override prepareRenderer method for doing that.

    Example:

    public Component prepareRenderer (TableCellRenderer renderer, int rowIndex, int columnIndex){  
    Component componenet = super.prepareRenderer(renderer, rowIndex, columnIndex);  
    
    if(getValueAt(rowIndex, columnIndex).toString().equals("Red")) {  
       componenet.setBackground(Color.RED);  
    } else if(getValueAt(rowIndex, columnIndex).toString().equals("Green")) {
       componenet.setBackground(Color.GREEN);
    }
    return componenet;
    } 
    

    UPDATE:

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    
    
    public class PrepareRendereEx {
    
    public static void main(String[] args) {
    
        JFrame frame = new JFrame();
        frame.setSize(new Dimension(400, 100));
    
        Object data[][] = { {"java", "j2ee"}, 
                            {"java", "j2ee"}, 
                            {"java", "j2ee"}
                          };
        Object[] columnNames = {"Java", "J2EE"};
    
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model) {
            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
                    int columnIndex) {
                JComponent component = (JComponent) super.prepareRenderer(renderer, rowIndex, columnIndex);  
    
                if(getValueAt(rowIndex, 0).toString().equalsIgnoreCase("java") && columnIndex == 0) {
                    component.setBackground(Color.RED);
                } else if(getValueAt(rowIndex, 1).toString().equalsIgnoreCase("j2ee") && columnIndex == 1){
                    component.setBackground(Color.GREEN);
                }
    
                return component;
            }
        };
    
        frame.add(new JScrollPane(table));
        frame.setVisible(true);
    }
    }
    

    OUTPUT:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how I can set gradient color in my layout.
I would like to set a custom color for the keyboard that is associated
I would like to know how to set up background color for a specific
I would like to set up a system that use both technologies at the
I would like to set up a very simple demo application that includes LINQ
I have a custom subclass of NSSearchField that I would like to set the
I would like to individually set cell background colors in an NSForm . I
Would do something like m.set('background-color', 'green') for m in @model.collection.where(selected: true) and then maybe
I would like to set the size and color of the text at random
I would like to know which property should I use to set background color

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.