MyCellRenderer.java Source code

Java tutorial

Introduction

Here is the source code for MyCellRenderer.java

Source

import java.awt.Component;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;

class MyCellRenderer extends JLabel implements ListCellRenderer {

    public MyCellRenderer() {
        setOpaque(true);
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        setText(value.toString());

        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        return this;
    }
}

public class Main {

    public static void main(String[] a) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JList list = new JList(new String[] { "A", "B", "C" });
        list.setCellRenderer(new MyCellRenderer());
        frame.add(new JScrollPane(list));

        frame.setSize(300, 200);
        frame.setVisible(true);
    }

}