Question

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!

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top