Applying a ListCellRenderer to individual cells on a JList

不打扰是莪最后的温柔 提交于 2019-12-08 11:01:45

问题


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?


回答1:


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?).




回答2:


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;
        }
    }
}


来源:https://stackoverflow.com/questions/15488883/applying-a-listcellrenderer-to-individual-cells-on-a-jlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!