-1

I have a JTable with 6 columns. When adding a row to the table I need to add black color for the last cell in fist row. red color for second row. The corresponding color comes from a different method. This is my custom table cell renderer.

class CustomRenderer extends DefaultTableCellRenderer
{
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column, Color color)
    {
        JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        label.setBackground(color);
        return label;
    }
}

This is how I used it when adding a table row.

  private void addTableRow(String type, String name, String rank, String notes, String location, Color color)
    {
        boolean isExport = isExportEnable();
        tableModel.addRow(new Object[]
        {
            type,
            name,
            rank,
            notes,
            location,
            isExport
        }
        );
        for (int i = 0; i < tableModel.getRowCount(); ++i)
        {
            JTable.getColumnModel().getColumn(6).setCellRenderer((TableCellRenderer) new CustomRenderer().getTableCellRendererComponent(null, null, false, false, i, 6, color));
        }
    }

When I add the fist row it gets the correct color. But when I add the second row both first and second color cell fills with the second color. How can I get the exact row. Actually what I want is to fill the last cell of each row with a different color while adding a row.

Ann
  • 21
  • 6
  • *"Although I posted this question earlier I did not get the exact answer."* What makes you think this one will get a different answer? Is it different in some way? Either way, you should [edit] an existing question rather than start a new one. – Andrew Thompson Jul 12 '18 at 10:26

1 Answers1

2

You need to go have a look at Concepts: Editors and Renderers and Using Custom Renderers because you clearly don't understand how renderers work in Swing.

You NEVER add a component to a JTable's TableModel, that's not it's responsibility.

You configure the JTable's TableColumns to use specific renders, something like...

table.getColumnModel().getColumn(6).setCellRenderer(new CustomRenderer);

assuming you want a specific renderer for a specific column and not have it configured for a type of object

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I got the solution but then faced to a different one. Changed the question with the correct solution and the new problem. – Ann Jul 12 '18 at 11:37
  • I am still struggling with the above question so can anyone help? – Ann Jul 16 '18 at 06:39
  • *"Actually what I want is to fill the last cell of each row with a different color while adding a row"* - Not really how it works. A column has a single render. What you need to do is seed the value of the cell with the color you want to use, then have the renderer change based on that value – MadProgrammer Jul 16 '18 at 07:38
  • Could you please show me an example? – Ann Jul 16 '18 at 08:27
  • [Something like this](https://stackoverflow.com/questions/30552644/how-do-i-color-individual-cells-of-a-jtable-based-on-the-value-in-the-cell/30553979#30553979)? – MadProgrammer Jul 16 '18 at 08:29
  • Thanks for your suggestion. But it did not work. Can I fill the color while adding the row instead of filling the cell after adding the row? – Ann Jul 16 '18 at 08:44
  • You have ONE cell renderer for each column. It takes data from the table model and "renderers" it the way you want to - this is how it works. So, you need to see the information you want into the row/column so that the render knows what to do with it – MadProgrammer Jul 16 '18 at 08:48
  • Still struggling. Can you please explain using my scenario? – Ann Jul 16 '18 at 12:14
  • Thanks. Finaly I made a solution I added colorInCellRenderer.setCellColor(tableModel.getRowCount() - 1, 6, color); JTable.repaint(); when adding a row. Now I have another problem I want to delete it when 'Delete' button is clicked. Is there any method to do that? – Ann Jul 18 '18 at 05:51