为什么在 ListCellRenderer 中需要 removeAll()? [英] Why removeAll() is required in ListCellRenderer?

查看:19
本文介绍了为什么在 ListCellRenderer 中需要 removeAll()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:-

public class MyRender extends JPanel implements ListCellRenderer {

    ImageIcon on_img;
    JLabel name = new JLabel();
    JLabel icn = new JLabel();
    JLabel img = new JLabel();

    public MyRender(Atalk) {
        setOpaque(true);
        setBackground(Color.WHITE);
        setForeground(Color.black);
        on_img = new ImageIcon(MyCls.class.getClassLoader().getResource("imgPath"));
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        if (value != null) {
            removeAll();
            setLayout(new BorderLayout());
            User user = (User) value;
            String pres = user.getPresence().toLowerCase();
            img.setIcon(default_img);
            if (pres.contains("unavailable"))
                icn.setIcon(off_img);
            else
                icn.setIcon(on_img);
            name.setText(user.getName());
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());

            add(img, BorderLayout.EAST);
            add(icn, BorderLayout.WEST);

            panel.add(st, BorderLayout.CENTER);
            panel.add(name, BorderLayout.NORTH);

            add(panel, BorderLayout.CENTER);

            JLabel lbl = new JLabel(" ");
            lbl.setSize(100, 5);
            add(lbl, BorderLayout.AFTER_LAST_LINE);

            if (isSelected) {
                setBackground(Color.lightGray);
                panel.setBackground(Color.lightGray);
            } else {
                setBackground(Color.white);
                panel.setBackground(Color.white);
            }

            return this;
        }
        return null;
    }
}

如您所见,我调用了 removeAll() 方法.如果我删除该行,则数据显示不正确.所有数据相互重叠.如果我添加 removeAll() 一切正常.为什么会发生这种情况?是否需要调用removeAll()?

As you can see I have called removeAll() method. If I remove that line the data is not displayed properly. All data overlaps each other. And If I add removeAll() all works fine. Why this happens? Is it necessary to call removeAll()?

推荐答案

您必须重组您的类,以便在构建时创建和添加 MyRender 的所有子级.

You have to restructure your class so that all children of MyRender are created and added at construction time.

getListCellRendererComponent() 应该用于更改现有组件的值或视觉属性(例如背景).

getListCellRendererComponent() should be used ONLY to change values or visual attributes (e.g. background) of existing components.

不要忘记 getListCellRendererComponent() 应该尽可能快(它可以被频繁调用),因此它不应该创建组件而只修改现有组件.

Don't forget that getListCellRendererComponent() should be as fast as possible (it can be called quite frequently), hence it should not create components but only modify existing ones.

通常,您的 getListCellRendererComponent() 方法如下所示:

Typically, here is how your getListCellRendererComponent() method should look like:

@Override
public Component getListCellRendererComponent(
    JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    if (value != null) {
        User user = (User) value;
        String pres = user.getPresence().toLowerCase();
        img.setIcon(default_img);
        if (pres.contains("unavailable"))
            icn.setIcon(off_img);
        else
            icn.setIcon(on_img);
        name.setText(user.getName());
        if (isSelected) {
            setBackground(Color.lightGray);
            panel.setBackground(Color.lightGray);
        } else {
            setBackground(Color.white);
            panel.setBackground(Color.white);
        }
    }
    return this;
}

这篇关于为什么在 ListCellRenderer 中需要 removeAll()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆