Search code examples
javaswingrenderjlistlistcellrenderer

Multiple ListCellRenderer


Is it possible to have multiple ListCellRenderer's implementation in a single class?

Actually I have multiple JList's in my application and I would I am in need of different ListCellRenderer's for each.

Can I have different class names for Implementing ListCellRenderer's Component method.

For ex: If I have a class with name "MultiColumnCellRenderer" with some implementation of Component method and another class with name "MultiColumnCellRenderer2" with some other implementation of Component method?

public class MultiColumnCellRenderer extends JPanel implements
        ListCellRenderer {

    public MultiColumnCellRenderer() {
    }

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

// Some implementation of Component Method

        super.setEnabled(list.isEnabled());
        super.setFont(list.getFont());

        return this;
    }
}


public class MultiColumnCellRenderer2 extends JPanel implements
        ListCellRenderer {

    public MultiColumnCellRenderer2() {
    }

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

// Some implementation of Component Method

        super.setEnabled(list.isEnabled());
        super.setFont(list.getFont());

        return this;
    }
}`

And if I do something like:

list1.setCellRenderer(new MultiColumnCellRenderer());
list2.setCellRenderer(new MultiColumnCellRenderer2());

Its not working out....

I am looking for different rendering for both list1 and list2.

How can I achieve this


Solution

  • Is it possible to have multiple ListCellRenderer's implementation in a single class?

    If by 'have' you mean 'use' and if by 'class' you mean 'GUI', then yes.

    Here is an example:

    MultiColumnCellRendererTest

    Source

    import java.awt.*;
    import javax.swing.*;
    
    class MultiColumnCellRendererTest {
    
        public static void main(String[] args) {
            final String[] fruits = {
                "Apple",
                "Pear",
                "Banana",
                "Grapefruit"
            };
    
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JList fruitList1 = new JList(fruits);
                    fruitList1.setCellRenderer(new MultiColumnCellRenderer());
    
                    JList fruitList2 = new JList(fruits);
                    fruitList2.setCellRenderer(new MultiColumnCellRenderer2());
    
                    JPanel gui = new JPanel(new GridLayout(1,0,2,2));
                    gui.add(fruitList1);
                    gui.add(fruitList2);
    
                    JOptionPane.showMessageDialog(null, gui);
                }
            });
        }
    }
    
    class MultiColumnCellRenderer extends JPanel implements
            ListCellRenderer {
    
        JLabel label;
    
        public MultiColumnCellRenderer() {
            setBackground(Color.RED);
            label = new JLabel();
            add(label);
        }
    
        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            label.setText(value.toString());
    
            super.setEnabled(list.isEnabled());
            super.setFont(list.getFont());
    
            return this;
        }
    }
    
    
    class MultiColumnCellRenderer2 extends JPanel implements
            ListCellRenderer {
    
        JLabel label;
    
        public MultiColumnCellRenderer2() {
            setBackground(Color.GREEN);
            label = new JLabel();
            add(label);
        }
    
        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            label.setText(value.toString());
    
            super.setEnabled(list.isEnabled());
            super.setFont(list.getFont());
    
            return this;
        }
    }