简体   繁体   中英

Color only specific cells in JTable

I'm looking for a solution for this problem: I have an excel file, that contains data. Some of the cells have yellow background. I already created a code for importing the text to JTable, which works fine. But I want to import the background-cell-color to specific cells also. For simplicity-sake of this example, I didn't use loops, reading the excel data from source etc. After reading the forum I understood I need CustomCellRenderer.

I have a problem with this approach, because this code colors the cells in the column correctly at first, but when I start to scroll over the colored cells in this table, it recolors the whole column to yellow. (see the screenshot)

I thought I could add else statement to specifically color the remaining cells to white, but this approach won't work for me, because I would be overwriting my previous cell results.

Can you point me to a solution on this one? (is this a bug, or expected behavior of JTable?). I'm using NetBeans and the GUI drag n drop generator

在此输入图像描述

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

public class MyRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        int[][] coordinatesYellow = new int[3][2];
        //[row][column] these cells are yellow
        coordinatesYellow[0][0] = 3;
        coordinatesYellow[0][1] = 2;
        coordinatesYellow[1][0] = 4;
        coordinatesYellow[1][1] = 2;
        coordinatesYellow[2][0] = 2;
        coordinatesYellow[2][1] = 2;

        for (int i = 0; i < 3; i++) {
            if ((row == coordinatesYellow[i][0]) && (column == coordinatesYellow[i][1])) {
                c.setBackground(Color.yellow);
            }
        }
        return c;
    }
} 



// And this is the statement I use for calling the renderer:
// resultsTable.getColumnModel().getColumn(0).setCellRenderer(new MyRenderer());

Make it so that when your cell should not be yellow, that you set the background to white (or the table's background color).

A renderer that extends DefaultTableCellRenderer uses the same component (a JLabel ) as a template for all cells (cf DefaultTableCellRenderer implementation notes - they call it rubber-stamping). Once you set its background to yellow, it will remain yellow for rendering consecutive cells until you change its background color again.

Replace your for loop with something like the following:

boolean isYellow = false;
for (int i = 0; i < 3; i++) {
            if ((row == coordinatesYellow[i][0]) && (column == coordinatesYellow[i][1])) {
                c.setBackground(Color.yellow);
                isYellow = true;
            }
        }
if( !isYellow )
  c.setBackground(Color.white);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM