0

I know there are a lot of questions on this subject but for some reason just cant find my way around this thing.

I need to change row color if two columns are not having the same values.

seen this link: http://tips4java.wordpress.com/2010/01/24/table-row-rendering/

but can't make it work....

tried this also:

public void colorRow(JTable table){
        for(int column=0;column<table.getRowCount();column++){
        for(int row=0;row<table.getRowCount();row++){
            TableCellRenderer renderer = table.getCellRenderer(row, column);
            Component comp = table.prepareRenderer(renderer, row, column);
            Float sinh = (Float)table.getModel().getValueAt(row,6);
            Float kol = (Float)table.getModel().getValueAt(row, 5);
            if(!kol.equals(sinh)){
                comp.setBackground(Color.YELLOW);
            }
        }
        }
    }

but it paints all the rows in the table if there is a row where columns 5 and 6 are not having the same value.

would appreciate any help

Update(SSCCE):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JScrollPane;
import javax.swing.JTable;

public class table extends JFrame {

    private JPanel contentPane;
    private JTable table;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    table frame = new table();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public table() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 495, 317);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(1, 0, 0, 0));

        DefaultTableModel model = new DefaultTableModel();

        model.addColumn("ID");
        model.addColumn("Value 1");
        model.addColumn("Value 2");

        int id1=1;
        int id2=2;
        int id3=3;
        Float one1=3.0f;
        Float two1=4.0f;
        Float one2=2.0f;
        Float two2=2.0f;
        Float one3=1.0f;
        Float two3=2.0f;

        model.insertRow(0, new Object [] {id1, one1,two1});
        model.insertRow(0, new Object [] {id2, one2,two2});
        model.insertRow(0, new Object [] {id3, one3,two3});

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane);

        table = new JTable(model);
        scrollPane.setViewportView(table);
        colorRow(table);

    }
    public void colorRow(JTable table){
        for(int column=0;column<table.getColumnCount();column++){
        for(int row=0;row<table.getRowCount();row++){
            TableCellRenderer renderer = table.getCellRenderer(row, column);
            Component comp = table.prepareRenderer(renderer, row, column);
            comp.setBackground(getBackground());
            Float sinh = (Float)table.getModel().getValueAt(row,2);
            Float kol = (Float)table.getModel().getValueAt(row, 1);
            if(!kol.equals(sinh)){
                comp.setBackground(Color.YELLOW);
            }
        }
        }
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
caniaskyouaquestion
  • 657
  • 2
  • 11
  • 21
  • 1
    `but can't make it work....` - Well, then post the [SSCCE](http://sscce.org/) that demonstrates the problem because we are not mind readers and can't guess what your code is. The code you posted won't work because once you change the background of the renderer it becomes the default color. Notice how the code in the link you where given always resets the background BEFORE doing the highlighting of the row color. – camickr Dec 10 '14 at 23:17
  • Why do people insist on overriding the `prepareRenderer` method? Why not just define an appropriate cell renderer(s) which can more easily be plugged into existing code :P – MadProgrammer Dec 10 '14 at 23:33
  • @camickr updated with an example..hope this helps figure out my problem.thanks – caniaskyouaquestion Dec 11 '14 at 07:41
  • use prepareRenderer, don't forget to convert indexes from view to mode – mKorbel Dec 11 '14 at 08:25
  • thanks...i have read through a lot of post here on stack, and conversations between mKorbel Troglod and cleopatra(not sure about the names)... the link that i have posted is gold, just needed to step away from my computer for a second. everything is working just fine now..thanks – caniaskyouaquestion Dec 11 '14 at 08:39
  • @mKorbel yes,using prepareRenderer ;) – caniaskyouaquestion Dec 11 '14 at 08:40
  • @caniaskyouaquestion, `updated with an example..` - where is the example that shows how you overrode the prepareRenderer() method. That code posted in the link works, so you customized it incorrectly. That is the example that you should have posted since you said it didn't work. – camickr Dec 11 '14 at 21:59

1 Answers1

1

I just tried the solution from this post with your SSCCE and it works. So delete the colorRow() method, then in your constructor replace the table initialization table = new JTable(model); with this:

    table = new JTable(model) {
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            Component comp = super.prepareRenderer(renderer, row, column);
            int modelRow = convertRowIndexToModel(row);
            Float sinh = (Float) getModel().getValueAt(modelRow, 2);
            Float kol = (Float) getModel().getValueAt(modelRow, 1);
            comp.setBackground(kol.equals(sinh) ? getBackground() : Color.YELLOW);
            return comp;
        }
    };

You fixed the problem I pointed out in the original code sample: for(int column=0; column < table.getRowCount(); column++){ by using the column count, but that didn't resolve the reason for your post.

Community
  • 1
  • 1
gknicker
  • 5,509
  • 2
  • 25
  • 41