/*  Sample custom Object and ListCellRenderer code for JEditList
    Illustrates how to customize JEditList with your own list item Objects
    and ListCellRenderer, including additional required methods needed by JEditList

    (C) Copyright 1998
    Wildcrest Associates
    http://www.wildcrest.com

    This source code may be freely used, modified, incorporated, and
    distributed without restriction as part of any software that uses
    JEditList by Wildcrest Associates.
*/

.
.
.

// create JScrollPane and JEditList
JScrollPane scrollPane = new JScrollPane();
scrollPane.setSize(150,200);
getContentPane().add(scrollPane);
JEditList list = new JEditList();
scrollPane.getViewport().add(list);

// read in some icons
Icon check = new ImageIcon("images/check.gif");
Icon ex = new ImageIcon("images/ex.gif");
Icon shapes = new ImageIcon("images/shapes.gif");

// define custom list item Objects
Vector data = new Vector();
data.addElement(new MyListItem("item 1", check));
data.addElement(new MyListItem("item 2", ex));
data.addElement(new MyListItem("item 3", ex));
data.addElement(new MyListItem("item 4", shapes));
data.addElement(new MyListItem("item 5", check));
data.addElement(new MyListItem("item 6", shapes));
data.addElement(new MyListItem("item 7", ex));
data.addElement(new MyListItem("item 8", check));
data.addElement(new MyListItem("item 9", ex));

// set JEditList to use these items
list.setListData(data);

// set JEditList to use custom ListCellRenderer
list.setCellRenderer(new MyListCellRenderer());

.
.
.


// class defining custom list item Objects
// including getItemString, setItemString, cloneItem methods required by JEditList
public class MyListItem {
    protected Icon icon;
    protected String text;
    public MyListItem(String t, Icon i) { icon = i; text = t; }
    public Icon getItemIcon() { return icon; }
    public String getItemString() { return text; }
    public void setItemString(String str) { text=str; }
    public Object cloneItem() { return new MyListItem(text, icon); }
}


// class defining corresponding custom ListCellRenderer
public class MyListCellRenderer extends Object implements ListCellRenderer {
    JLabel theLabel;

    public MyListCellRenderer() {
        theLabel = new JLabel();
        theLabel.setOpaque(true);
    }

    public Component getListCellRendererComponent(
                            JList list,
                            Object value,
                            int index,
                            boolean isSelected,
                            boolean cellHasFocus) {
        MyListItem li=null;
        if (value instanceof MyListItem) li = (MyListItem) value;

        theLabel.setFont(list.getFont());

        if (isSelected) {
            theLabel.setForeground(list.getSelectionForeground());
            theLabel.setBackground(list.getSelectionBackground());
        }
        else {
            theLabel.setForeground(list.getForeground());
            theLabel.setBackground(list.getBackground());
        }

        if (li != null) {
            theLabel.setText(li.getItemString());
            theLabel.setIcon(li.getItemIcon());
        }
        else {
            theLabel.setText(value.toString());
        }

        return theLabel;
    }
}
