Search code examples
javaswingjlabeljlist

Adding JLabels to a JList?


So far, I've tried simply going through my labels and adding each one to the list's list model with:

listModel.addElement(label);

So this works, but it simply gives a string representation of the label rather than the actual label.

Basically, is it possible to have a JList of JLabels? If not, then what's a good way to have a scrollable list of JLabels that I can update?


Solution

  • JList, JTable, JComboBox's popup return by default JLabel / JComponent from the Renderer, then there is no reason to put those JComponents types to the JList (in this case)

    but it simply gives a string representation of the label rather than the actual label
    
    • please read how Renderer to works, JLabel and Icon, examples here and here,

    • concept of Renderers is similair for JList, JTable, JComboBox's popup