简体   繁体   English

仅对JTable中的特定单元进行着色

[英]Color only specific cells in JTable

I'm looking for a solution for this problem: I have an excel file, that contains data. 我正在寻找这个问题的解决方案:我有一个包含数据的excel文件。 Some of the cells have yellow background. 一些细胞具有黄色背景。 I already created a code for importing the text to JTable, which works fine. 我已经创建了一个代码,用于将文本导入JTable,工作正常。 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. 为简单起见,这个例子,我没有使用循环,从源等读取excel数据。阅读论坛后我明白我需要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. 我以为我可以添加else语句来专门将剩余的单元格着色为白色,但这种方法对我来说不起作用,因为我会覆盖以前的单元格结果。

Can you point me to a solution on this one? 你能指点我这个解决方案吗? (is this a bug, or expected behavior of JTable?). (这是一个bug,还是JTable的预期行为?)。 I'm using NetBeans and the GUI drag n drop generator 我正在使用NetBeans和GUI拖放生成器

在此输入图像描述

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). 扩展DefaultTableCellRenderer的渲染器使用相同的组件( JLabel )作为所有单元格的模板(参见DefaultTableCellRenderer实现说明 - 他们称之为橡皮图章)。 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: 用以下内容替换for循环:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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