Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JList customization :o)

Posted on 2004-09-05
10
Medium Priority
?
296 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

972 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