Search code examples
java-melwuit

Alternative for cloning in LWUIT Component object


Situation:-

In my code I have to use the LWUIT Component object for the listview controls. The controls are dynamic and hence can be in any number. Right now I am creating Component objects according to the controls(in numbers) i.e.- for every control to be created first the Component object is creating. This process slows down the rendering of the listview when the controls are increasing.

Solution:-

If I create the Component object and use it in a loop for all the controls it is taking the reference of the object and hence displays all the listview items(controls) with the same data. Now I am able to think of one last option of Cloning my object and using it to create the controls.

Problem:-

But I can't find any way in LWUIT by which I can achieve the copying of object.

What can be the alternatives in LWUIT to solve this problem?

P.S.-The listview items are of same type, but with different data.


Solution

  • Use a List component and the Renderer design pattern to create a "rubber stamp" component where you can display a large number of elements easily. See an explanation of this in the Codename One blog.

    Create these classes first :

    public class ListUtil {
    
        private Vector data = new Vector();
        private Content[] contents;
    
        public ListUtil(Vector vData)
        {
            data = vData;
            contents = new Content[vData.size()];
        }
    
        public List createList(Display display, CListCell renderer, ActionListener listener)
        {
            CList theList;
            for(int i = 0; i < data.size(); i++)
            {
                contents[i] = new Content(String.valueOf(data.elementAt(i)));
            }
            theList = new CList(display, contents, renderer, listener);
            return theList;
        }
    }
    
    public class Content
    {
        private String  row;
    
        public Content(String row)
        {
            this.row = row;
        }
    
        public String getRow()
        {
            return (row);
        }
    }
    
    public class CListCell extends Container implements ListCellRenderer {
    
        private Label focus = new Label("");
    
        public CListCell()
        {
            super();
            // create and add the components here among the components which will display data
        }
        public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
        {
            Content entry = null;
            if (value instanceof Content)
                entry = (Content)value;
            componentDisplayingDataAddedIntoThisListCellRenderer.setText(entry.getRow());
            return this;
        }
        public Component getListFocusComponent(List arg0)
        {
            return focus;
        }
    }
    
    public class CList extends List {
        private Display disp;
        public CList(Display display, Object[] data, CListCell renderer, ActionListener actionListener)
        {
            super(data);
            setListCellRenderer(renderer);
            setIgnoreFocusComponentWhenUnfocused(true);
            addActionListener(actionListener);
            setScrollAnimationSpeed(getScrollAnimationSpeed()/4);
            disp = display;
        }
        public void pointerReleased(int x,int y)
        {
            if (isEnabled() && hasFocus())
                super.pointerReleased(x, y);
        }
        public void keyReleased(int keyCode)
        {
            if (isEnabled() && hasFocus())
            {
                if (disp.getGameAction(keyCode) == Display.GAME_FIRE)
                    pointerReleased(getX(),getY());
                else
                    super.keyReleased(keyCode);
            }
        }
    }
    

    To create your List and add it to a Form :

    public class myForm extends Form implements ActionListener
    {
        private Vector listSource = // your vector of data
        private CListCell renderer = new CListCell();
        private List theList = (new ListUtil(listSource)).createList(Display.getInstance(),renderer, this);
        ...
        public void actionPerformed(ActionEvent evt)
        {
           if (evt.getSource() == theList)
                doSomething();
        }
    }