5

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ahamed
  • 39,245
  • 13
  • 40
  • 68

2 Answers2

7

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

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
4

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    Read the tutorial. You where given the link in the other answer. The secion on how to use lists has more infomration about renderers. – camickr Jan 15 '12 at 16:16