Search code examples
javahtmlswingjcomboboxrenderer

Dual color JComboBox text


In Swing is there any way to have two colors for single option? I mean, if I have a option, say Show only cities [Alt+C] in JComboBox, I want Show only cities text in one color and [Alt+C](hotkey) in another color. Similarly, I have JList, Which contains a list of string items. I want to differentiate one from other by color.

In short,

  1. How to design two colored JComboBox option.
  2. How to differentiate JList objects with color.

Thanks.


Solution

  • YES , use html,

    See this for more information ,

    How to Use HTML in Swing Components

    For your jcomboBox,

    jComboBox.addItem("<html><font color=green>Show only cities <font color=red>[Alt+C]</font></html>");
    

    will work :) cheers