0

I have a standard com.sun.lwuit.list. I can get the selected item using methods getSelectedItem or getSelectedIndex. The item is a picture and two labels. How do I know if I clicked on the picture or on one of the labels. I admit that it is possible to pass the click event to a child components or perhaps may exists a method for finding component by current mouse coordinates.

public class NewsFeedListRender extends Container implements ListCellRenderer 
{
    private final Container newsFeedCont = new Container();
    private final Container pictureCont = new Container();
    private final Label name = new Label();
    private final Label message = new Label();
    private final Label picture = new Label();
    private final Label data    = new Label();
            ....

    public NewsFeedListRender() 
    {   
        setLayout(new BorderLayout());
        newsFeedCont.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        pictureCont.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        Style s = name.getStyle();
        s.setFont(font_large);          

        s = message.getStyle();
        s.setFont(font_small);

        s = data.getStyle();
        s.setFont(font_mini);
                .....
            }

   }

    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) 
    {       
        if (value instanceof MessageItem) 
        {           
            MessageItem newsFeedData = (MessageItem) value;
            if (newsFeedData.getSender() != null)
            {
                if (newsFeedData.getSender().getName() != null)
                    name.setText(newsFeedData.getSender().getName()); //fixthis
            }
            else
            {
                name.setText("Unknown sender");
            }
            if(newsFeedData.getMessage() != null)
                message.setText(newsFeedData.getMessage());
            else
            {
                message.setText("Default message");
            }

            try 
            {
                data.setText(newsFeedData.getDataReceive().toString());
            } 
            catch (Exception e) 
            {
                System.out.println(e.toString());
            }                               
            Image img = null;
            img = newsFeedData.getSender().getIcon();
            if( img != null)
            {
                picture.setIcon(img);
            }
                        .......
Maxim Bjjtwins
  • 108
  • 1
  • 8

1 Answers1

2

getSelectedItem() returns the Container object and you can count the value of the Container object. Then you need to get the what are the components you are added into this container. See the sample code,

Container con = (Container) list.getSelectedItem();
for(int i = 0; i < con.getComponentCount(); i++){
Object obj = (Object) con.getComponentAt(i); // typecast component name instead object
}
bharath
  • 14,283
  • 16
  • 57
  • 95
  • I already know what components include in item. I want to know how determinate if I clicked on the picture or on one of the labels ? – Maxim Bjjtwins Sep 16 '11 at 08:31
  • AFAIK You add the listener for list. So you can't find which component you are clicked in the list item. – bharath Sep 28 '11 at 07:34