Solved

JList customization :o)

Posted on 2004-09-05
10
286 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple java question 3 59
Apps blocked by Java 9 88
printf performancy 11 66
Crystal Reports Licensing Questions 4 33
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

791 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