Is it possible to have a different font colors on one JLabel inside in the JList?

papski picture papski · Sep 8, 2011 · Viewed 7.6k times · Source

I have a list of words in my Jlist and beside of every words are their definitions. I want that the font of the words are having a different colors than their definition. My question is that, Is it possible to have a two different colors in one Jlist?

Do I have to use ListCellRenderer?

Thanks...

Answer

Yasin Okumuş picture Yasin Okumuş · Sep 8, 2011

I think you may use "<html>" style. It might look some odd but if you start your text (String value) by "<html>" (not capital letter HTML), you will be able to use html codes on your labels.

As an example;

new JLabel("<html><font color=red>RED</font> - <font color=navy>Navy</font></html>");

Similar for JList.