?
Solved

JList customization :o)

Posted on 2004-09-05
10
Medium Priority
?
295 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 100 total points
ID: 11983885
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 100 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 100 total points
ID: 11986468
0
Technology Partners: 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 100 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

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!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

719 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