java-melwuit-form

How to Set an image to lwuit list?


i want to set a title and an image to lwuit list from Rss feed,i am able to set title ,but i dont know how to set an image?after i set an image and title ,then i need to display it on form... here my code:,Help...

public void disp() {
          //String[] items={newsItem.getTitle()};
        for(int i=0;i<news.size();i++){


          newsItem=(News)news.elementAt(i);

         myNewsList.addItem(newsItem.getTitle().toString());
          System.out.println(newsItem.getTitle());


        }
        try{
                  System.out.println("hiii");

        form1.addComponent(myNewsList);

        form1.addCommand(cmdDetails);
        form1.setScrollable(true);
        form1.setTransitionInAnimator(Transition3D.createRotation(250, true));

          form1.show();
        }
        catch(Exception e){
            e.printStackTrace();
        }

Solution

  • You can use list renderer to add image and text in single list item.

    NewsListRenderer.java

    public class NewsListRenderer implements ListCellRenderer {
    
        private Label lblImage;
        private TextArea textAreaHeadline;
    
        public Component getListCellRendererComponent(List arg0, Object obj,
                int arg2, boolean isSelected) {
    
            Container newsContainer = new Container();
            newsContainer.setLayout(new BoxLayout(BoxLayout.X_AXIS));
    
            News newsListObj = (News) obj;
            Image img = newsListObj.getThumbnail();
            lblImage = new Label(img);
            lblImage.getStyle().setBgTransparency(0);
            lblImage.setTextPosition(Component.BOTTOM);
            newsContainer.addComponent(lblImage);
    
            textAreaHeadline = new TextArea(3, 25);
            textAreaHeadline.setSelectedStyle(textAreaHeadline.getStyle());
            textAreaHeadline.setText(newsListObj.getHeadLine());
            textAreaHeadline.setEditable(false);
            textAreaHeadline.getStyle().setBorder(null);
            textAreaHeadline.setFocusable(false);
            textAreaHeadline.getStyle().setBgTransparency(0);
            newsContainer.addComponent(textAreaHeadline);
            return newsContainer;
        }
    
        public Component getListFocusComponent(List arg0) {
            return new Container();
        }
    }
    

    Members of News Class:

    1) News title - getHeadLine() to retrieve title.

    2) News Image - getThumbnail() to retrieve image.

        myNewsList = new List(news);
        myNewsList.setListCellRenderer(new NewsListRenderer());
        form1.addComponent(myNewsList); 
        form1.addCommand(cmdDetails);
        form1.setScrollable(true);
        form1.setTransitionInAnimator(Transition3D.createRotation(250, true));
        form1.show();