Solved

JList customization :o)

Posted on 2004-09-05
10
282 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
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 25 total points
Comment Utility
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 25 total points
Comment Utility
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
Comment Utility
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 25 total points
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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
Comment Utility
ThanQ all
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
glad to help :)
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now