I'm making enhancements to a Swing app (never done Swing programming before), and need to be able to make a single text item in a JList
bold. I've seen a few posts where they said to just put "<html><b>"
and "</b></html>"
around the string. Are you serious, that seems like such a hack. It's also possible in the future that we'll want to change the background color of an item in the JList - would that also be possible with HTML tags?
Another suggestion I've seen is to call the setCellRenderer()
method on the JList with your own object that implements the ListCellRenderer
interface. But I'm not sure that can do what we want. It seems the ListCellRenderer
has a getListCellRendererComponent()
method where you set how an item will be displayed depending on whether it is selected or has the focus. But we want to have one item in the JList be bold depending on what our business logic determines, and it may be an item that is neither selected nor has the focus. I haven't seen any good examples of this ListCellRenderer
, so I'm not sure if it's the approach we want.