Java – JTable cellrenderer changes the backgroundColor of a cell at run time
I am trying to create a table and color specific cells, yellow, red or white, depending on the contents of other columns To do this, I cycle through the rows of values and check the contents It works well for each line currently displayed on the screen, but when the program reaches a line that is not displayed, or if the user tries to scroll each cell, change its background color to white I searched the Internet for a solution. The only reasonable idea is to reset the cellrenderer after each cycle. This doesn't work because it will also reset each cell
I want someone to know the solution, or let me know where I screwed up
I'm using this loop
for(int e = 0; e < modules.size(); e++) { gui.clearOutputStream(); gui.getOutputStream().setText("Load Modul " + modules.get(e) + "\r\n"); version = getVersion(modules.get(e)); //Update current Row updateRow(gui.getReleaseTabelle(),e); }
It calls this method
public void updateRow(JTable target,int row){ //... //insert Values here //... CustomRenderer cr = new CustomRenderer(); cr.tab = target; if(!target.getValueAt(row,2).equals(target.getValueAt(row,3))) { cr.Val1 = target.getValueAt(row,1).toString(); target.setValueAt("X",row,1); } else if(!target.getValueAt(row,7).equals("")) { cr.Val1 = target.getValueAt(row,1).toString(); target.setValueAt("Y",1); } else { } target.getColumnModel().getColumn(1).setCellRenderer(cr); }
This is my customrenderer
class CustomRenderer extends DefaultTableCellRenderer { private static final long serialVersionUID = 6703872492730589499L; public String Val1; public JTable tab; public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) { Component cell = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,column); if(tab.getValueAt(row,1).equals("Y")){ cell.setBackground(new java.awt.Color(255,255,0)); tab.setValueAt(Val1,1); } else if(tab.getValueAt(row,1).equals("X")){ cell.setBackground(new java.awt.Color(255,50,50)); tab.setValueAt(Val1,1); } else { //do nothing } return cell; } }
Solution
Do not update table data in the cutomrenderer class The Ren renrer class should check the condition and color the cells I use the customrenderer class and render cells based on the data in the cells If the cell's data is "Y", turn it yellow If the data is "n", set its color to gray
import java.awt.Color; import java.awt.Component; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; public class ColoringCells { private static Object[] columnName = {"Yes","No"}; private static Object[][] data = { {"Y","N"},{"N","Y"},{"Y","N"} }; public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { JFrame frame = new JFrame(); JTable table = new JTable(data,columnName); table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer()); table.getColumnModel().getColumn(1).setCellRenderer(new CustomRenderer()); frame.add(new JScrollPane(table)); frame.setTitle("Rendering in JTable"); frame.pack(); frame.setVisible(true); } }; EventQueue.invokelater(r); } } class CustomRenderer extends DefaultTableCellRenderer { private static final long serialVersionUID = 6703872492730589499L; public Component getTableCellRendererComponent(JTable table,int column) { Component cellComponent = super.getTableCellRendererComponent(table,column); if(table.getValueAt(row,column).equals("Y")){ cellComponent.setBackground(Color.YELLOW); } else if(table.getValueAt(row,column).equals("N")){ cellComponent.setBackground(Color.GRAY); } return cellComponent; } }