Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

java - Dynamically changing contents of ListCellRenderer

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.

This is the code to retrieve a tweet

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

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?

Thanks for the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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,

  • Get all of the statuses at the beginning
  • Pass them as an array to a JList
  • Then create a custom ListCellRenderer class and in your getListCellRendererComponent method, return your JLabel which has your ListCellRenderer code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...