开发者

LWUIT item question

开发者 https://www.devze.com 2023-04-05 11:55 出处:网络
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

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 Contain开发者_StackOverflow社区er 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);
            }
                        .......


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号