动态更改ListCellRenderer的内容 [英] Dynamically changing contents of ListCellRenderer

查看:68
本文介绍了动态更改ListCellRenderer的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Twitter4构建一个Twitter客户端.我将用户的tweet和信息等存储在Jlist的DefaultListModel中.我想添加用户个人资料图片,并为此进行操作,请使用ListCellRenderer设置图标.我的问题是,我只能将ListCellRenderer文本和图标设置为一个用户信息.我使用循环下拉多个tweet并将其添加到模型中,但是渲染器仅设置了一条tweet多次.

I am attempting to build a twitter client using Twitter4. I am storing the users tweets and info etc in a DefaultListModel in a Jlist. I want to add the users profile picture and to do this I am setting the Icon using a ListCellRenderer. My issue here is that I am only able to set the ListCellRenderer text and icon to one users information. I use a loop to pull down multiple tweets and add them to the model, but the renderer is only setting one tweet many times.

这是检索推文的代码

for (int i = 0; i < list.size(); i++) {
 Status each = (Status) list.get(i);

                    UI.model.addElement("<html><body style='width: 450px;'>"
                            + "@"
                            + each.getUser().getScreenName()
                            + " - "
                            + each.getText() + "<html><br>");

                    UI.whatIsDisplayedList.setCellRenderer(new newsFeedRenderer(each)); }

这就是我设置ListCellRenderer的方式

And this is how I am setting the ListCellRenderer

JLabel pic = new JLabel();

        try {
            ImageIcon img = new ImageIcon(TwitterFunctions.eachTweetProfilePic(each.getUser()));
            pic.setIcon(img);
            setIcon(img);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        setText( "@" + each.getUser().getScreenName() + " - " + each.getText());

要对推文进行正确的格式化,我必须进行哪些修改?

What modifications would I have to make to enable the correct formatting of the tweets?

感谢您的帮助!

推荐答案

您不应将newFeedReader()传递给setCellRenderer();ListCellRenderer是用于绘制单元格的对象,不能用作数据库类型的对象.

You shouldn't pass a newFeedReader() to setCellRenderer(); A ListCellRenderer is an object used to paint cells, not to be used as a database kind of object. What you're going to want to do is,

  • 在开始时获取所有状态
  • 将它们作为数组传递给JList
  • 然后创建一个自定义ListCellRenderer类,并在您的getListCellRendererComponent方法中,返回包含ListCellRenderer代码的JLabel

这篇关于动态更改ListCellRenderer的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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