Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 260
Ahamed Avatar asked Jan 15 '12 15:01

Ahamed


2 Answers

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

like image 51
COD3BOY Avatar answered Oct 14 '22 11:10

COD3BOY


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.

like image 22
Hovercraft Full Of Eels Avatar answered Oct 14 '22 13:10

Hovercraft Full Of Eels