LWUIT项目问题

时间:2011-09-16 07:57:55

标签: java-me lwuit

我有一个标准com.sun.lwuit.list。我可以使用getSelectedItemgetSelectedIndex方法获取所选项目。该项目是一张图片和两个标签。如何点击图片或其中一个标签,我怎么知道。我承认可以将click事件传递给子组件,或者可能存在一种通过当前鼠标坐标查找组件的方法。

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

1 个答案:

答案 0 :(得分:2)

getSelectedItem()返回Container对象,您可以计算Container对象的值。然后,您需要获取添加到此容器中的组件。请参阅示例代码

Container con = (Container) list.getSelectedItem();
for(int i = 0; i < con.getComponentCount(); i++){
Object obj = (Object) con.getComponentAt(i); // typecast component name instead object
}