多个ListCell渲染器

发布于 2024-12-29 04:45:40 字数 1426 浏览 2 评论 0原文

是否可以在单个类中拥有多个 ListCellRenderer 的实现?

实际上,我的应用程序中有多个 JList,并且每个 JList 都需要不同的 ListCellRenderer。

我可以使用不同的类名来实现 ListCellRenderer 的 Component 方法吗?

例如:如果我有一个名为“MultiColumnCellRenderer”的类,具有 Component 方法的某些实现,而另一个类名为“MultiColumnCellRenderer2”,具有 Component 方法的某些其他实现?

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

如果我做类似的事情:

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

它不起作用......

我正在为 list1 和 list2 寻找不同的渲染。

我怎样才能实现这个目标

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薆情海 2025-01-05 04:45:40

是否可以在一个类中实现多个ListCellRenderer?

如果“拥有”的意思是“使用”,而“类”的意思是“GUI”,那么是的。

这是一个示例:

MultiColumnCellRendererTest

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

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;
    }
}
勿挽旧人 2025-01-05 04:45:40

这是一个有效的 SSCCE - 也许您可以弄清楚这与您自己的代码有何不同。

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;
import javax.swing.ListCellRenderer;
public class TwoLists {
    private static void list(ListCellRenderer renderer, Object... elements) {
        DefaultListModel model = new DefaultListModel();
        for(Object element : elements) {
            model.addElement(element);
        }

        JList list = new JList(model);
        list.setCellRenderer(renderer);

        JFrame frame = new JFrame();
        frame.getContentPane().add(new JScrollPane(list));
        frame.setSize(200, 200);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        list(new DefaultListCellRenderer() {
            public Component getListCellRendererComponent(JList list, Object value,
                    int index, boolean isSelected, boolean cellHasFocus) {
                Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                c.setBackground(Color.RED);
                return c;
            }
        }, "One", "Two", "Three");

        list(new DefaultListCellRenderer() {
            public Component getListCellRendererComponent(JList list, Object value,
                    int index, boolean isSelected, boolean cellHasFocus) {
                Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                c.setBackground(Color.BLUE);
                return c;
            }
        }, "North", "South", "EAST");
    }
}

Here's an SSCCE that works - maybe you can figure out how this is different from your own code.

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;
import javax.swing.ListCellRenderer;
public class TwoLists {
    private static void list(ListCellRenderer renderer, Object... elements) {
        DefaultListModel model = new DefaultListModel();
        for(Object element : elements) {
            model.addElement(element);
        }

        JList list = new JList(model);
        list.setCellRenderer(renderer);

        JFrame frame = new JFrame();
        frame.getContentPane().add(new JScrollPane(list));
        frame.setSize(200, 200);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        list(new DefaultListCellRenderer() {
            public Component getListCellRendererComponent(JList list, Object value,
                    int index, boolean isSelected, boolean cellHasFocus) {
                Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                c.setBackground(Color.RED);
                return c;
            }
        }, "One", "Two", "Three");

        list(new DefaultListCellRenderer() {
            public Component getListCellRendererComponent(JList list, Object value,
                    int index, boolean isSelected, boolean cellHasFocus) {
                Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                c.setBackground(Color.BLUE);
                return c;
            }
        }, "North", "South", "EAST");
    }
}
心房敞 2025-01-05 04:45:40

还可以安排单个 ListCellRenderer 实现来管理指定的 Color。在下面的变体中,使用 darker() 颜色来表示选择。

import java.awt.*;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/8987285 */
class MultiColumnCellRendererTest {

    private static final String[] fruits = {
        "Apple", "Pear", "Banana", "Grapefruit"
    };

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new GridLayout(1, 0, 2, 2));
                gui.add(createList(Color.red));
                gui.add(createList(Color.green));
                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }

    private static JList createList(Color color) {
        JList list = new JList(fruits);
        list.setCellRenderer(new ColorCellRenderer(color));
        return list;
    }

    private static class ColorCellRenderer extends JLabel implements ListCellRenderer {

        private Color background;
        private Color selected;

        public ColorCellRenderer(Color background) {
            this.setOpaque(true);
            this.background = background;
            this.selected = background.darker();
            this.setBackground(background);
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
            setText(value.toString());
            setBackground(isSelected ? selected : background);
            return this;
        }
    }
}

It's also possible to arrange for a single ListCellRenderer implementation to manage a specified Color. In the variation below, a darker() color is used to signify selection.

import java.awt.*;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/8987285 */
class MultiColumnCellRendererTest {

    private static final String[] fruits = {
        "Apple", "Pear", "Banana", "Grapefruit"
    };

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new GridLayout(1, 0, 2, 2));
                gui.add(createList(Color.red));
                gui.add(createList(Color.green));
                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }

    private static JList createList(Color color) {
        JList list = new JList(fruits);
        list.setCellRenderer(new ColorCellRenderer(color));
        return list;
    }

    private static class ColorCellRenderer extends JLabel implements ListCellRenderer {

        private Color background;
        private Color selected;

        public ColorCellRenderer(Color background) {
            this.setOpaque(true);
            this.background = background;
            this.selected = background.darker();
            this.setBackground(background);
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
            setText(value.toString());
            setBackground(isSelected ? selected : background);
            return this;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文