0

I'm trying to change the JTable cell color (foreground). It is working but it's coloring the cell+1 and not the required cell (as you see in my code).

I'm trying to change the color of the current row and column 3 but, it's actually changing the color of next column.

This code is added in the custom code.

BaritemsTable = new javax.swing.JTable(){
@Override
public Component prepareRenderer (TableCellRenderer renderer, int rowIndex , int columnIndex ){
    Component component = super.prepareRenderer(renderer , rowIndex , columnIndex );
    Object value = getModel().getValueAt(rowIndex , columnIndex);

    if (columnIndex == 3){

        if (value.equals("Ready")){
            BaritemsTable.setForeground(new java.awt.Color(51, 204, 0));
            BaritemsTable.setFont(new Font("Tahoma", Font.PLAIN, 48));
        }
        if (value.equals("Process")){
            BaritemsTable.setForeground(new java.awt.Color(51, 51, 255));
            BaritemsTable.setFont(new Font("Tahoma", Font.PLAIN, 48));
        }
        if (value.equals("Queued")){
            BaritemsTable.setForeground(new java.awt.Color(255, 0, 0));
            BaritemsTable.setFont(new Font("Tahoma", Font.PLAIN, 48));
        }

    } else {
        BaritemsTable.setForeground(new java.awt.Color(0, 0, 0));
        BaritemsTable.setFont(new Font("Tahoma", Font.PLAIN, 48));
    }
    return component;
  }
};
TT.
  • 15,774
  • 6
  • 47
  • 88
  • Indices always start at 0, so column 3 has the columnIndex 2 not 3 – tgallei Feb 17 '20 at 07:21
  • I'm starting from index 0 – NeedHelpJava Feb 17 '20 at 07:55
  • 1) Looks like a job for a [`TableCellRenderer`](https://docs.oracle.com/en/java/javase/13/docs/api/java.desktop/javax/swing/table/TableCellRenderer.html). 2) For better help, post a [mre]. 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Feb 17 '20 at 10:09

1 Answers1

0

Problem one in your code, is definitely this:

Object value = getModel().getValueAt(rowIndex , columnIndex);

The indexes you are receiving in handlers and listener, methods etc in JTable are view indexes and can only be used to index the view. In other words, you should write:

Object value = getValueAt(rowIndex , columnIndex); // Use JTable.getValueAt

The second problem: when referencing a column index directly in prepareRenderer you should be using a view index in that spot. You most likely need

if (convertColumnIndexToModel(columnIndex) == 3)

JTable.convertColumnIndexToModel makes sure you are using an index from the model. Why is all this needed, and what is a view index and model index? How do they relate? I gave a bit more explanation here.


The third problem: if you want to set colors and fonts for a row/cell you need to set them on the component returned from super.prepareRenderer, not on the table. For instance:

if (value.equals("Ready")){
    component.setForeground(new java.awt.Color(51, 204, 0));
    component.setFont(new Font("Tahoma", Font.PLAIN, 48));
}
TT.
  • 15,774
  • 6
  • 47
  • 88
  • I'm starting from index 0, and i've changed my code to your suggestion Object value = getValueAt(rowIndex , columnIndex); with no effect!!! – NeedHelpJava Feb 17 '20 at 07:59
  • I didn't look further than the problems I listed (I'm doing this as a quick break from work). What I listed is definitely legit. But you are trying to set colors using `BaritemsTable.setForeground` but that's not how that's done. You should set those properties on the component, not on the table. I'll update my answer. – TT. Feb 17 '20 at 08:01
  • I did that, but I'm getting an error non-static method setForeground color cannot be referenced from static-context!! – NeedHelpJava Feb 17 '20 at 08:13
  • @NeedHelpJava I cannot see what you did, but I can tell you that you didn't apply what I outlined correctly. There are no static contexts in what I wrote in my answer. – TT. Feb 17 '20 at 08:18
  • Good to hear @NeedHelpJava. Please take the time to accept my answer by checking the check mark (✔) next to it. Read more about this etiquette here: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – TT. Feb 17 '20 at 08:31