Search code examples
javaarraysswingjtablejlabel

How do I add a JLabel[] to a JTable?


I have an array of JLabels which I want to add to a JTable. I tried using

 myJTable.add(myJLabelArray);

Hoping it would work, but it doesn't (Obviously, otherwise I wouldn't be here).

Can somebody please help?


Solution

  • Using add method is not the way to add components to a JTable. Components should never be added directly to a JTable or its TableModel.

    JLabels are just Swing components that render text.

    You can use a TableCellRenderer. Have a look at Editors & Renderers