简体   繁体   English

将ListCellRenderer应用于JList上的单个单元格

[英]Applying a ListCellRenderer to individual cells on a JList

Is it possible to apply a listcellrenderer to purely one cell in a jlist ? 是否有可能一个应用listcellrenderer纯粹一个细胞在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? 因此,总结一下-我只想将listcellrenderer应用于列表中的一个单元格,我该怎么做?

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. 您必须将ListCellRenderer应用于列表中的所有元素,但这并不意味着它必须以相同的方式呈现所有元素。 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;
        }
    }
}

Is it possible to apply a listcellrenderer to purely one cell in a jlist? 是否可以将listcellrenderer应用于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? 因此,总结一下-我只想将listcellrenderer应用于列表中的一个单元格,我该怎么做?

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 (eg, render differently how?). 有关更详细的答案,请考虑创建和发布sscce并进行更多说明(例如,如何进行不同的渲染?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM