-1

note: this code is not mine, I have taken it from another site and i'm simply trying to modify it.

I have a JTable with a load of details however, I want it so that when I change a particular cell for the first cell to change colour. Currently this code just highlights the row when I click on it, but I want it so that if I change one of the values to another number, the name cell for example to change red. I have tried a few things (if statements) but can't seem to work it. Any help would be great.

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class CustomCellRenderer{
   JTable table;
   TableColumn tcol;
   public static void main(String[] args) {
   new CustomCellRenderer();
   }

  public CustomCellRenderer(){
   JFrame frame = new JFrame("Creating a Custom Cell Reanderer!");
   JPanel panel = new JPanel();
   String data[][] = {{"Vinod","Computer","3"},
    {"Rahul","History","2"},
    {"Manoj","Biology","4"},
    {"Sanjay","PSD","5"}};
   String col [] = {"Name","Course","Year"};
   DefaultTableModel model = new DefaultTableModel(data,col);
   table = new JTable(model);
   tcol = table.getColumnModel().getColumn(0);
   tcol.setCellRenderer(new CustomTableCellRenderer());
   tcol = table.getColumnModel().getColumn(1);
   tcol.setCellRenderer(new CustomTableCellRenderer());
   tcol = table.getColumnModel().getColumn(2);
   tcol.setCellRenderer(new CustomTableCellRenderer());
   JTableHeader header = table.getTableHeader();
   header.setBackground(Color.yellow);
   JScrollPane pane = new JScrollPane(table);
   panel.add(pane);
   frame.add(panel);
   frame.setSize(500,150);
   frame.setUndecorated(true);
   frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);
   }

  public class CustomTableCellRenderer extends DefaultTableCellRenderer{
   public Component getTableCellRendererComponent (JTable table, 
 Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
   Component cell = super.getTableCellRendererComponent(
    table, obj, isSelected, hasFocus, row, column);
   if (isSelected) {
   cell.setBackground(Color.green);
   } 
   else {
   if (row % 2 == 0) {
   cell.setBackground(Color.lightGray);
   }
   else {
   cell.setBackground(Color.lightGray);
   }
   }
   return cell;
   }
   }
 } 
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • the code doesn't seem to contain the part you want to achieve? The renderer does nothing but striping, first implement it color dependent on the value that should determine the visuals, then make sure to repaint the cells that need repainting. – kleopatra Mar 26 '13 at 13:33

2 Answers2

1

If you know row number you want to highlight just add in the end of the getTableCellRendererComponent method

if (row==theRowNumberToHighlight && column=0) {
  cell.setForeground(Color.red);
}
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • I want it to highlight just a cell "dynamically". So if a value changes in a specific cell then the colour of a specific cell to change... –  Mar 26 '13 at 13:01
  • So you must know where value is changed and what are the row/column of the changed value – StanislavL Mar 26 '13 at 13:29
1

Assuming your table model extends AbstractTableModel, extend TableModelListener. Use the following tableChanged method to figure out when to call your renderer:

public void tableChanged(TableModelEvent e)  
{  
    if (e.getColumn() == columnYouAreChecking && e.getFirstRow() == rowYouAreChecking && e.getLastRow() == rowYouAreChecking)
    {
        // Change cell color here.
    }  
}

This code will get called every time the data in your table changes.

Eric Hydrick
  • 3,467
  • 2
  • 28
  • 41