Search code examples
javajtablejpanelcellimageicon

Changing Content of JTable Cells Individually


So Suppose I create a JTable with a specific number of columns and rows. Is it possible to add a JPanel to a specific cell after the JTable is already initialized? The JTable would be empty at first and then I would change a cell at row X and column Y to instead contain a JPanel which would contain an amount of ImageIcons. I don't have any code to put here really, I just need to know what I would have to use to accomplish this and how. Thank you.


Solution

  • To change the data in the table you use:

    table.setValueAt(...);
    

    This will update the data in the TableModel.

    However, adding a JPanel to the table is not the way a table is designed to be used. Typically you add data to the model. Then you use a renderer to display the data in a cell.

    Start by reading the section from the Swing tutorial on Using Custom Renderers for more information and examples.