Solved

JList customization :o)

Posted on 2004-09-05
10
288 Views
Last Modified: 2010-03-31
Hi there,
is it possible to customize a JList?
I'm creating an IM for a client, and for all the contacts that come on, I would like to be added to a JList. How much customization to the JList can I do? Can I change the font (colour, face and size)? Also, can I change the colour of the 'selected' JList component?
Anything on how to customize a JList would be much appreciated.

Thanks in advance!!
-R
0
Comment
Question by:rubberdi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 25 total points
ID: 11983885
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 25 total points
ID: 11983888
Yes, if practised long before you create the JList.
I did not test it myself, and may be there's more than:

               UIManager.put("List.foreground", new ColorUIResource(int red, int green, int blue);
               UIManager.put("List.background", new ColorUIResource(int red, int green, int blue);
               UIManager.put("List.selectionForeground", new ColorUIResource(int red, int green, int blue);
               UIManager.put("List.selectionBackground", new ColorUIResource(int red, int green, int blue);
               UIManager.put("List.font", new Font(.....));

;JOOP!
0
 
LVL 92

Assisted Solution

by:objects
objects earned 25 total points
ID: 11986468
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 25 total points
ID: 11987646
Creating your own list cell renderer is the way to go.
That's quite easy:

/*
 * MyListCellRenderer.java
 *
 */

import javax.swing.*;
import java.awt.*;
/**
 *
 * @author  zzynx
 */
public class MyListCellRenderer extends JLabel implements ListCellRenderer {
   
    /** Creates a new instance of MyListCellRenderer */
    public MyListCellRenderer() {
        setOpaque(true);
        setFont(new Font("Verdana", Font.BOLD|Font.ITALIC, 12));       // set your specific font
    }
   
    public java.awt.Component getListCellRendererComponent(JList list,
                Object value, int index, boolean isSelected, boolean cellHasFocus) {

        if  (value != null)
                setText(value.toString());
        setBackground(isSelected ?
                      Color.RED : list.getBackground());     // Selected = RED color
        setForeground(isSelected ?
                      list.getSelectionForeground() : list.getForeground());  // You can adapt the foreground color too
        return this;        
    }
}

In your other code:

      yourJList.setCellRenderer( new MyListCellRenderer() );

Remark: sciuriware's suggestions does also work, but then it is for *ALL* list boxes in your application
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11987654
check out an MSN style list made in java
The project is an open source, so you can take help of making a renderer from it
http://jmsn.sourceforge.net/
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11988414
There is some good reason to have all your components of certain kind look the same.
It's less confusing to the user. That's why SUN createde the UI properties.
;JOOP!
0
 

Author Comment

by:rubberdi
ID: 12045256
ThanQ all
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12045274
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12048879
glad to help :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12051710
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
by zero exception 10 69
How do I remove an object from a 3 52
Which non-HTML GUI front end to use with Java? 3 52
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question