Paint the rows of a JTable according to a condition - Java

0

I have a JTable tbDocTransfer with 17 columns full of information, what I want is to paint each row that meets the following condition: if in the first column of the JTable rows it says "E" it is painted red and if it does not say nothing is painted in Black. I've been trying with this code but the moment I execute it does not change.

void pintarFila(){
        String fila;
        for(int i=0; i<tbDocTransferir.getRowCount(); i++){
            fila = ""+tbDocTransferir.getValueAt(i, 0);
            if (fila == "E")
                tbDocTransferir.setForeground(Color.RED);
            else if (fila == " ")
                tbDocTransferir.setForeground(Color.BLACK);
        }
    }
    
asked by Leandro 23.02.2017 в 21:21
source

1 answer

2

You want to paint the Row of your JTable.

Create a new class that inherits from DefaultTableCellRenderer to handle the render of your cells.

package ejemplo;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;


public class RowsRenderer extends DefaultTableCellRenderer {
private int columna ;

public RowsRenderer(int Colpatron)
{
    this.columna = Colpatron;
}

@Override
public Component getTableCellRendererComponent (JTable table, Object value, boolean selected, boolean focused, int row, int column)
{        
    setBackground(Color.white);
    table.setForeground(Color.black);
    super.getTableCellRendererComponent(table, value, selected, focused, row, column);
    if(table.getValueAt(row,columna).equals("A"))
    {
        this.setForeground(Color.RED);
    }else if(table.getValueAt(row,columna).equals("B")){
        this.setForeground(Color.BLUE);
    }else if(table.getValueAt(row, columna).equals("C")){
        this.setForeground(Color.GREEN);
    }
    return this;
  }
  }

Class main of the example:

package ejemplo;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class JPanelDemo extends JFrame {
private String columnas[] = {"Auto", "Color", "Tipo"};
private Object celdas[][] = {{"Kia","Rojo", "C"},
    {"Toyota","Azul","C"},
    {"Lexus","Negro","B"},
    {"BMW","Verde","B"},
    {"Pagani", "Dorado", "A"},
    {"Ferrari", "Rojo", "A"}
};
private JTable tabla;

public JPanelDemo(){
    super("JTable Color");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    setLocationRelativeTo(null);
    tabla = new JTable(celdas,columnas);
    RowsRenderer rr = new RowsRenderer(2);
    tabla.setDefaultRenderer(Object.class, rr);
    add(new JScrollPane(tabla));
    setVisible(true);
    pack();
}

public static void main(String[]args){
    JPanelDemo obj = new JPanelDemo();
}

}
  • If the car is type C, then the row text will be painted in Green.
  • If the car is type B, then the text in the row will be painted Blue.
  • If the car is type A, then the text in the row will be painted in Red.
answered by 23.02.2017 / 23:05
source