0

I am writing a utility that will compare metadata fields from a SQL database. I am able to get all of the data into a JTable. I am trying to now color each particular cell depending on if they match the cell of another JTable. From my understanding I need to write a CellRenderer (Change the color of specific rows in my JTable). I had thought doing something like the below to compare the two values would be the best solution

for(int i=0;i<col2;i++){
                for(int j=0;j<row2;j++){
                     if(table1.getValueAt(i,j).equals(table2.getValueAt(i,j))){
                        table1.setSelectionBackground(Color.GREEN);
                        table2.setSelectionBackground(Color.GREEN);
                    }else if(!(table1.getValueAt(i, j).equals(table2.getValueAt(i, j)))){
                        table1.setSelectionBackground(Color.RED);
                        table2.setSelectionBackground(Color.RED);
                    }
                }
            }

I know the that the setSelectionBackground is not the method I want to call. I'm confused on how to write the CellRenderer listed in the above post to change the background color of a cell in depending if the contents match each other. Is writing the custom CellRenderer the only option?

EDIT 1: As of right now, it appears to be taking in the correct color for background but it is coloring the entire table rather than a specific cell. Below is my CellRenderer and one of the for loops for how I think I should be calling the setBackground method

    private class CellRenderer extends DefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected,boolean hasFocus,int row, int col){
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
        this.setOpaque(true);
        this.setBackground(table.getBackground());              
        return this;
    }

    int col1 = table1.getColumnCount()-1;
    int row1 = table1.getRowCount()-1;
    int col2 = table2.getColumnCount()-1;
    int row2 = table2.getRowCount()-1;
    table1.setDefaultRenderer(Object.class, new CellRenderer());
    table2.setDefaultRenderer(Object.class, new CellRenderer());
    if(row1>row2){
        if(col1>col2){
            for(int i=0;i<row2;i++){
                for(int j=0;j<col2;j++){
                    if(table1.getValueAt(i,j).equals(table2.getValueAt(i,j))){
                        color = Color.GREEN;
                        System.out.println(color);

                        table1.setBackground(color);
                        table2.setBackground(color);
                    }else if(!(table1.getValueAt(i, j).equals(table2.getValueAt(i, j)))){
                        color = Color.RED;
                        System.out.println(color);
                        table1.setBackground(color);
                        table2.setBackground(color);
                    }
                }
            }
Community
  • 1
  • 1
  • I think you have to subclass DefaultTableCellRenderer and then set it as your Table's cellRenderer and do there what you want to do. I have an example class used by a program doing what you want, I can post it here if you haven't found a solution yet. – tur1ng Mar 08 '17 at 20:58
  • Would this interfere with me overriding a prepareRenderer method used by my JTable to get everything to fit inside the cell? I have this method much further above in my code after the JTable is created as a submethod? i dont think thats the correct name but its basically JTabel foo = new JTabel(model){ prepareRenderer....}; – Caleb Schwartz Mar 08 '17 at 21:03
  • 1
    A `TableCellRenderer` that invokes `setBackground()` is the correct approach; you can synchronize scrolling like [this](http://stackoverflow.com/q/2614457/230513). For specific guidance, please edit your question to include a [mcve] that exhibits any remaining problem. – trashgod Mar 09 '17 at 11:22

0 Answers0