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?
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
There is (almost) never a need to put a JComponent
in the model side of a Swing component (JList
, JTree
, JTable
, ... ). In your ListModel
you put the Object
s you want to visualize in a JList
, and you let the renderer take care of the representation.
So if you want to represent each Object
as a special label you create that label in your renderer.
See the Swing list tutorial for more information. This tutorial does not contain a custom renderer, but instead refers you to the custom renderer section of the combobox tutorial, which is very similar
JList
already presents you like a list of labels ( non editable items that you can change via code). You may want to update your question with more detail. And, the DefaultListCellRenderer already extends JLabe - ring bearer 2012-04-05 17:38