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

时间:2017-01-02 11:55:36

标签: java swing jtable tablecellrenderer defaulttablemodel

我的问题很简单,但我搜索了整个互联网和StackOverflow,但无法找到完美的答案,所以我在这里。我是通过JTable上的类以编程方式创建JForm,并在DefaultTableModel中添加JTable来保存数据,然后在DefaultTableModel中逐个添加行如下。

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

我想在添加注释代码的地方更改颜色,以便我该怎么做? 我有一个想法,我将保存这些行没有所需的颜色,然后在结束,只是立即渲染整个表?有可能做到这一点吗?

更新

我使用了下面的代码,但它的工作方式几乎符合我的要求,但它没有按行保存在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);

并增加了一个班级......

// 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;
    }
}

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

2 个答案:

答案 0 :(得分:2)

您的TableModel应存储在为该单元格调用renderer时确定单元格颜色所需的信息。在这个完整的example中,每个Row都有一个state,以及自定义TableCellRenderer查询,用于确定要显示的颜色。此相关example说明了如何使用TableModel创建自定义Map

答案 1 :(得分:2)

最后,我能够在其他人的帮助下做我想做的事。我使用下面的代码,它按照我的愿望工作,并按行保存在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);

并增加了一个班级......

// 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;
    }
}

感谢您阅读...: - )