Question

Is it possible to apply a listcellrenderer to purely one cell in a jlist? My code currently works fine in applying the renderer, but I would like to set a different dynamic variable for each entry. Apologies if this is a little vague..

So to sum up - I want to apply listcellrenderer to only one cell in a list, how would I do this?

Was it helpful?

Solution 2

Is it possible to apply a listcellrenderer to purely one cell in a jlist?

No, all cells would have to share the same renderer. That's how renderers work.

My code currently works fine in applying the renderer, but I would like to set a different dynamic variable for each entry.

This can be done. The renderer can change how it renders the cell depending on the state of the data that it's supposed to render.

Apologies if this is a little vague..

It's always better if you explain more and show code.

So to sum up - I want to apply listcellrenderer to only one cell in a list, how would I do this?

Again have the renderer's behavior depend on the value held by the cell. For a more detailed answer, consider creating and posting an sscce and explaining more (e.g., render differently how?).

OTHER TIPS

You have to apply the ListCellRenderer to all elements in the list, but that does not mean it has to render all of them in the same way. For example, you could render a cell based on its value (either the raw value or even just based on the value's class, or even based on the cell's index:

package com.example;

import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class ListCellRendererExample extends JFrame {

    public ListCellRendererExample() {
        DefaultListModel model = new DefaultListModel();
        model.addElement(Color.BLUE);
        model.addElement("hello");
        model.addElement(5);
        model.addElement(Color.RED);

        JList jlist = new JList(model);
        jlist.setCellRenderer(new SuperDuperListCellRenderer());
        add(new JScrollPane(jlist));

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new ListCellRendererExample();
    }

    private static class SuperDuperListCellRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {

            // If the value is a color, give the cell a blank value but save its
            // value so we can later change its background to the value's color.
            Color bgColor = null;
            if (value instanceof Color) {
                bgColor = (Color) value;
                value = " ";
            }

            // Prepend the index to the "even" rows (the first row is row 1)
            if ((index + 1) % 2 == 0) {
                value = index + ": " + value;
            }

            Component renderComponent = super.getListCellRendererComponent(
                    list, value, index, isSelected, cellHasFocus);

            // If the value is a color, set the cell's background to that color.
            if (bgColor != null) {
                renderComponent.setBackground(bgColor);
            }

            return renderComponent;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top