0

I am trying to implement a button such that when it is clicked, the color of a cell in that row changes colors. I have a cellRenderer class:

public class MyCellRenderer extends    javax.swing.table.DefaultTableCellRenderer {
public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    cellComponent.setBackground(java.awt.Color.RED);
    return cellComponent;
}

}

that gets invoked as follows:(for column 15 let's say):

MyCellRenderer mcr = new MyCellRenderer();
Mytable.getColumnModel().getColumn(15).setCellRenderer(mcr);

notice how it gets invoked on a ColumnModel object. Is there a method to select a cell (row, column) and change its color based on the coordinates. Such that it can be invoked by a JTable object? For example:

Mytable.colorCell(1,7); //colors cell in row 1, column 7

Thank you

Shabka
  • 21
  • 2
  • Possible duplicate of http://stackoverflow.com/questions/24555289/changing-cell-color-without-changing-color-in-other-cells-jtable and many others – Marco13 Jan 22 '15 at 20:47
  • none actually mention or have a method invoked on a specific cell for a JTable object, I've been looking all morning however your answer helps, thank you! – Shabka Jan 22 '15 at 20:50
  • @Marco13 In the link that you posted how exactly did you "assign renderer to table"? and then use that renderer later as an object? Can you please give an example on how to implement it in a main program for example – Shabka Jan 22 '15 at 21:18
  • The first code snippet in my answer there (the `CellRendererTest`) is a complete example, that can be compiled and executed. The renderer is created as `final ColoringCellRenderer cellRenderer = new ColoringCellRenderer();`, and assigned to all columns with `column.setCellRenderer(cellRenderer);`. – Marco13 Jan 22 '15 at 21:21

0 Answers0