如何在向模型添加行时更改 JTable 行的背景颜色?

How To Change JTable Rows Background Color While Adding Rows To Model?

我的问题很简单,但我在整个互联网和 Whosebug 上进行了搜索,但找不到完美的答案,所以我来了。我正在从 JForm 上的 Class 以编程方式创建 JTable,并在我的 JTable 中添加 DefaultTableModel 以保存数据,然后在 [= 中逐行添加行16=] 如下。

private JTable desiredJTable = new JTable();
private DefaultTableModel tableModel = new DefaultTableModel();
private List<String> myArrayOne= new ArrayList<String>();
private List<String> myArrayTwo= new ArrayList<String>();
private Map<String, Integer> myDictionaryOne= new HashMap<String, Integer>();
private Map<String, Integer> myDictionaryTwo= new HashMap<String, Integer>();

我添加的行如下所示,所以我不知道单色行的固定长度。

int RowIndex = 0;
int i = 0;
for (String word : myDictionaryOne.keySet())
{
    if (myArrayOne.contains(word) == true)
    {
        tableModel.addRow(new Object[] { word });
        //tableModel.setRowColour(RowIndex, Color.GREEN);                        
        RowIndex++;
        i++;
    }
    if (i >= 10)
    {
        break;
    }
} 

int i = 0;
for (String word : myDictionaryTwo.keySet())
{
    if (myArrayTwo.contains(word) == true)
    {
        tableModel.addRow(new Object[] { word });
        //tableModel.setRowColour(RowIndex, Color.RED);                        
        RowIndex++;
        i++;
    }
    if (i >= 10)
    {
        break;
    }
} 

我想在添加注释代码的地方更改颜色,我该怎么做? 我有一个想法,制作一个 class,我将在其中保存这些行没有所需的颜色,然后在最后,一次渲染整个 table?有什么办法可以做到这一点?

已更新:

我使用了下面的代码,但它几乎按照我的意愿工作,但它没有按照保存在 ArrayList 中的方式在行中显示正确的颜色...

CustomRenderer colouringTable = new CustomRenderer();
int RowIndex = 0;
int i = 0;
for (String word : myDictionaryOne.keySet())
{
    if (myArrayOne.contains(word) == true)
    {
        tableModel.addRow(new Object[] { word });
        //tableModel.setRowColour(RowIndex, Color.GREEN);
        colouringTable.setColors(Color.GREEN);                           
        RowIndex++;
        i++;
    }
    if (i >= 10)
    {
        break;
    }
} 

int i = 0;
for (String word : myDictionaryTwo.keySet())
{
    if (myArrayTwo.contains(word) == true)
    {
        tableModel.addRow(new Object[] { word });
        //tableModel.setRowColour(RowIndex, Color.RED);
        colouringTable.setColors(Color.RED);                         
        RowIndex++;
        i++;
    }
    if (i >= 10)
    {
        break;
    }
} 
desiredJTable.getColumnModel().getColumn(0).setCellRenderer(colouringTable);

并添加了一个class...

// JTable Colouring Class
class CustomRenderer extends DefaultTableCellRenderer 
{
    private List<Color> desiredColors = new ArrayList<Color>();
    private static final long serialVersionUID = 6703872492730589499L;

    public void setColors(Color incomingColor)
    {
        desiredColors.add(incomingColor);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        // This Code Is Not Working Perfectly | Its Colouring All With Same Last Color...
        for (int i = 0; i < desiredColors.size(); i++) {
            cellComponent.setBackground(desiredColors.get(i));
        }
        /*
        // This Code Is Working
        if(row == 0){
            cellComponent.setBackground(Color.YELLOW);
        } else if ( row == 1){
            cellComponent.setBackground(Color.GRAY);
        } else {
            cellComponent.setBackground(Color.CYAN);
        }
        */
        return cellComponent;
    }
}

那么现在如何解决这个问题...???

renderer is called for that cell. In this complete example, each Row has a state, and the custom TableCellRenderer queries that state to determine the color to display. This related example 说明如何使用 Map 创建自定义 TableModel 时,您的 TableModel 应该存储确定单元格颜色所需的信息。

我终于可以在其他人的帮助下做我想做的事了。我使用了下面的代码,它按照我的意愿工作,并按照保存在 ArrayList 中的方式在行中显示正确的颜色...

CustomRenderer colouringTable = new CustomRenderer();
int RowIndex = 0;
int i = 0;
for (String word : myDictionaryOne.keySet())
{
    if (myArrayOne.contains(word) == true)
    {
        tableModel.addRow(new Object[] { word });
        colouringTable.setColors(Color.GREEN);                           
        RowIndex++;
        i++;
    }
    if (i >= 10)
    {
        break;
    }
} 

int i = 0;
for (String word : myDictionaryTwo.keySet())
{
    if (myArrayTwo.contains(word) == true)
    {
        tableModel.addRow(new Object[] { word });
        colouringTable.setColors(Color.RED);                         
        RowIndex++;
        i++;
    }
    if (i >= 10)
    {
        break;
    }
} 
desiredJTable.getColumnModel().getColumn(0).setCellRenderer(colouringTable);

并添加了一个class...

// JTable Colouring Class
class CustomRenderer extends DefaultTableCellRenderer 
{
    private List<Color> desiredColors = new ArrayList<Color>();

    public void setColors(Color incomingColor)
    {
        desiredColors.add(incomingColor);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        for (int i = 0; i < desiredColors.size(); i++) {
            if(row == i){
                cellComponent.setBackground(desiredColors.get(i));
            }
        }
        return cellComponent;
    }
}

感谢阅读...:-)