2

I have a JList and inside that list, I want to change the color of the font of the following words.

Someone said that I have to use CellRenderer but I'm not yet familiar with that.

Now, my problem is how can I setup this renderer to change the color of the Font?

Any idea about this matter?

Thanks...

mKorbel
  • 109,525
  • 20
  • 134
  • 319
papski
  • 1,241
  • 5
  • 28
  • 52
  • 2
    suggest to always have a tab open with http://download.oracle.com/javase/tutorial/uiswing/index.html - and quickly read the how-to-use xx chapter (with xx being the component you don't know yet :-) – kleopatra Sep 07 '11 at 10:49
  • is it possible to make two different colors in one JLabel inside a list? – papski Sep 07 '11 at 12:38
  • 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? – papski Sep 08 '11 at 01:25

3 Answers3

6

Check out the JavaDoc, it might just have what you need (the example at the top). Basically you just return a component (like a JLabel) with a configured font color.

Edit: since you want to have the words in a different color than their definitions, here two possibilities how that can be done with the list cell renderer:

  1. Return a JPanel that contains 2 JLabels, one with the word and one with the definitions. Both labels can have different foreground colors.
  2. JLabel supports HTML (like other Swing components do), so you might just set the text of the label as <html><font color=red>your word</font> - your definitions</html>. Note that the default foreground color should then be the one for the definitions, alternatively you can wrap the definitions with a <font> tag as well. Have a look at this Tutorial.
Thomas
  • 87,414
  • 12
  • 119
  • 157
  • 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? – papski Sep 08 '11 at 01:34
  • 1
    @Mikel yes, this is possible. I'll add two possibilities. – Thomas Sep 08 '11 at 07:54
6

For JLabel components try:

    new DefaultListCellRenderer(){
        @Override
        public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) { 
            JLabel label = (JLabel)super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
            label.setForeground( Color.RED );
            return label;
        }  
    };
oliholz
  • 7,447
  • 2
  • 43
  • 82
  • 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? – papski Sep 08 '11 at 01:27
2

consider ...., that would be better using a JTable with one TableColumn and without TableHeader as the JList, demonstrated here, here, by using prepareRenderer, because JList has lots of restriction/missed methods as JTable

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    - 1 for over-complicating ;-) The question was clearly about JList and the way to configure it's cell appearance is a .. well ... custom renderer implemenatation. – kleopatra Sep 07 '11 at 10:45
  • and I enjoyed that just @Andrew Thompson gets it from you, today:-) – mKorbel Sep 07 '11 at 10:50