11
Jun
09

how to display any type of Object in a JList



If you’re like me, and you have used JList before, you used it with objects of the String class. What do we do if we want to display other type of objects ?

I had to display some File objects in the list, and I didn’t want to resort to adding a String to the model, because I really wanted to be able to pass the File object back & forth in my code. A bit of googling revealed that the solution was to use a custom ListCellRenderer. If you open their page, you’ll notice they are showing an example there.

I wrote a class I’m using in my project. Here’s the code :


import java.awt.*;
import javax.swing.*;

public abstract class AbstractedCellRenderer extends JLabel implements ListCellRenderer {

    protected abstract String getPaintedStringFrom(Object value);
    private Color selectedForegroundColor,selectedBackgroundColor;

    public AbstractedCellRenderer()
    {
        selectedForegroundColor = Color.BLUE;
        selectedBackgroundColor = Color.RED;
        unselectedBackgroundColor = Color.WHITE;
        unselectedForegroundColor = Color.BLACK;
    }

    public Color getSelectedBackgroundColor() {
        return selectedBackgroundColor;
    }

    public void setSelectedBackgroundColor(Color selectedBackgroundColor) {
        this.selectedBackgroundColor = selectedBackgroundColor;
    }

    public Color getSelectedForegroundColor() {
        return selectedForegroundColor;
    }

    public void setSelectedForegroundColor(Color selectedForegroundColor) {
        this.selectedForegroundColor = selectedForegroundColor;
    }

    public Color getUnselectedBackgroundColor() {
        return unselectedBackgroundColor;
    }

    public void setUnselectedBackgroundColor(Color unselectedBackgroundColor) {
        this.unselectedBackgroundColor = unselectedBackgroundColor;
    }

    public Color getUnselectedForegroundColor() {
        return unselectedForegroundColor;
    }

    public void setUnselectedForegroundColor(Color unselectedForegroundColor) {
        this.unselectedForegroundColor = unselectedForegroundColor;
    }
    private Color unselectedForegroundColor,unselectedBackgroundColor;

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setText(getPaintedStringFrom(value));
        if(isSelected)
        {
            setForeground(selectedForegroundColor);
            setBackground(selectedBackgroundColor);
        }
        else
        {
            setForeground(unselectedForegroundColor);
            setBackground(unselectedBackgroundColor);
        }
        return this;
    }

}

To use it, extend it just like in the following example:



import java.awt.*;
import java.io.File;
import javax.swing.*;

public class FileObjectCellRenderer extends AbstractedCellRenderer
{

    @Override
    protected String getPaintedStringFrom(Object value) {
        File file = (File)value;
        return file.getName();
    }

}

The thing is, I’m displaying a String, by overriding the getPaintedStringFrom, but this way, we get to choose where that String comes from. You could show an image as well, by setting an ImageIcon on the JLabel.

Hope this helps you as well!

Advertisements

0 Responses to “how to display any type of Object in a JList”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 228,119 hits

%d bloggers like this: