1

I have been seen many articles but I don't understand how to do it!

I want to change the color of some rows in JTable. The table has 3 columns: a, b and c.

The rules

  1. If the value of a<=b the color of the entire row must be red
  2. If the value of a>=c the color of the entire row must be yellow
  3. in default the color of row must be blue.
Community
  • 1
  • 1
user3684431
  • 29
  • 1
  • 5
  • 1
    Cell rendering is control by the `TableCellRenderer`, you can learn more about those at [Concepts: Editors and Renderers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) and [Using Custom Renderers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer). If you really want something easier, you could take a look at the `JXTable` from the SwingLabs libraries, which provides row highlighting – MadProgrammer Jul 23 '14 at 03:45
  • I understand that but when i try implement a custom rendersr it take a lot of problem sometimes the entires table colored – user3684431 Jul 23 '14 at 03:56
  • *"it take a lot of problem sometimes the entires table colored"* 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). 2) Always copy/paste error or exception output. – Andrew Thompson Jul 23 '14 at 03:57
  • @user3684431 Then you're not resetting the state of the render between calls – MadProgrammer Jul 23 '14 at 04:01
  • please can you help me to make a custom rendeer? – user3684431 Jul 23 '14 at 04:04
  • 1
    possible duplicate of [JTable row color depending on Value in model?](http://stackoverflow.com/questions/8137766/jtable-row-color-depending-on-value-in-model) – DavidPostill Jul 23 '14 at 06:38

1 Answers1

1

Try the following code

public class IconifiedRenderer extends JLabel implements TableCellRenderer {
public IconifiedRenderer() {
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    JLabel label = this;
    int cellValueA=-1;
    int cellValueB=-1;
    int cellValueC=-1;
    try {
        setOpaque(true);
        label.setText(String.valueOf(value));
       try {
            cellValueA = Integer.parseInt(String.valueOf( table.getValueAt(row, 0))); //0th for A
        } catch (ArrayIndexOutOfBoundsException aa) {
            //ignore
            cellValueA=-1;
        }
       try {
            cellValueB = Integer.parseInt(String.valueOf( table.getValueAt(row, 1))); //1th for A
        } catch (ArrayIndexOutOfBoundsException aa) {
            //ignore
            cellValueB=-1;
        }
       try {
            cellValueC = Integer.parseInt(String.valueOf( table.getValueAt(row, 2))); //2th for A
        } catch (ArrayIndexOutOfBoundsException aa) {
            //ignore
            cellValueC=-1;
        }
       label.setBackground(Color.BLUE);

       if(cellValueA<=cellValueB){
           label.setBackground(Color.RED);
       }
       if(cellValueA>=cellValueC){
           label.setBackground(Color.YELLOW);
       }
    } catch (Exception ex) {
        // no need to handle
    }
    return label;
}

Add this render class and set the render on you table column

    jTable1.getColumnModel().getColumn(0).setCellRenderer(new IconifiedRenderer());
    jTable1.getColumnModel().getColumn(1).setCellRenderer(new IconifiedRenderer());
    jTable1.getColumnModel().getColumn(2).setCellRenderer(new IconifiedRenderer());

It will show your table like this...

enter image description here

Tej Kiran
  • 2,218
  • 5
  • 21
  • 42
  • Tej Kiran Think you think you think you are the best – user3684431 Jul 23 '14 at 08:10
  • This approach only works if all the data in all the columns is String data. What if you have Dates, Numbers, Booleans, then you wil need to repeat the code in multiple different renderers. For a solution that does not require multiple renderer check out the link provided by David in the comments section. – camickr Jul 23 '14 at 13:51