Question

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.

Was it helpful?

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

OTHER TIPS

Yes, I believe that this can be done with HTML or failing that, by using a cell renderer for your combo box that allows for this such as a JEditorPane.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top