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

1.8k views Asked by At

My question is simple but I searched all over the internet and StackOverflow but not able to find out the perfect answer so I am here. I am creating a JTable programmatically from a Class on a JForm and adding DefaultTableModel in my JTable for holding data then adding rows one by one in DefaultTableModel as follows.

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

And I am adding rows like below so I dont know about the fix length of single color rows.

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

I want to change color where I added the commented code so How can I do this? I have an ideas to make a class where I will save these rows no with there desired color and later in the End, just render the whole table at once? Anything is possible to do this?

Updated:

I used the below code but its working nearly as per my desire but its not showing the right color in the rows as per saved in the 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);

And added a 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;
    }
}

So How to fix this now...???

2

There are 2 answers

0
Sahar Jabeen On BEST ANSWER

Finally I am able to do what I want with the help of other guys. I used the below code and its working as per my desire and showing the right color in the rows as per saved in the 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);

And added a 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;
    }
}

Thanks for reading... :-)

2
trashgod On

Your TableModel should store the information needed to determine the cell's color when the 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 illustrates how to create a custom TableModel using a Map.