Java - Custom ListCellRenderer

In Java you get a lot of Swing user control but sometimes the standard look doesn't really satisfy your demands. You want some more information in it or want a different layout of the component. Today I shall give a small example of what you can do with a JList. Basicly you can display anything that fits in a JPanel. So that means you literally can display anything inside a JList element. 

This is a small example of a custom listcellrenderer which displays an image along with some information about that image.

 
To get this result we need to create a class which implements the 'ListCellRenderer' interface and extends the JPanel class. For this interface we need to override the 'getListCellRendererComponent' method. It has the following signature: 'public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)'.
 
Short code preview of this Custom ListCellRenderer:
 

public class ImageListRenderer extends JPanel implements ListCellRenderer {

private static final long serialVersionUID = 1L;

 

private JLabel icon;

private JLabel title;

private JTextArea description;

private Color evenRowColor = Color.white;

private Color oddRowColor = new Color(235, 235, 235);

public Component getListCellRendererComponent(JList list, Object value,

int index, boolean isSelected, boolean cellHasFocus) {

 

if (isSelected) 

{

    setBackground(list.getSelectionBackground());

    setForeground(list.getSelectionForeground());

   

    title.setBackground(list.getSelectionBackground());

    title.setForeground(list.getSelectionForeground());

   

    description.setBackground(list.getSelectionBackground());

    description.setForeground(list.getSelectionForeground());

}

else 

{

Color c = ((index & 0x1) == 1) ? evenRowColor : oddRowColor;

setBackground(c);

setForeground(list.getForeground());

title.setBackground(c);

title.setForeground(list.getForeground());

   

description.setBackground(c);

    description.setForeground(list.getForeground());

}

ListObject item = (ListObject)value;

icon.setIcon(item.getIcon());

title.setText(item.getTitle());

description.setText(item.getDescription());

return this;

}

..... more code ....

This is the class that renders all list elements. It has a couple of labels and a textarea in it. Just basic stuff, but you can put anything in it. If you implement the ListCellRenderer interface you need to implement all the code when an element is clicked that it changes color. In more complex renderers this can be a quite a pain in the ass. But it's certainly worth it !
 
The JList component needs to know that you want to use your own custom listcellrenderer. This only takes one single line of code.

 

JList imageList = new JList();

imageList.setCellRenderer(new ImageListRenderer()); 
 
 
A reminder though! If you place buttons in it the are displayed but the click events DONT work! 
 
 
Hope this will help you create your own listcellrenderers. I know this is small and simple but this is all it takes to make your controls really be usefull. 

You can download the whole example below. It's an Eclipse project file with all files in it. 

CustomListCellRenderer.zip (33.07 kb)

July 25, 2008 14:49 by sander
E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

BluBlu

In my search for something new and exciting on the internet I came across a very cool website of an street artist/painter. He makes amazing paintings on buildings throughout the world. Really Amazing. Just check out his website and especially the movies section. Just to give you a preview of what he makes.

http://blublu.org 

July 24, 2008 12:16 by sander
E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed