1

I have four buttons at the bottom of my form. Clicking on any of these buttons results into a container preparation. In this container I've five components(includes buttons) and this container can be in three states(changes on button click which is inside the container).

By three states I mean every time I press a button which is added to this container as a component I need to replace a few/ add one or two components and then display the container again.

Now, here I'm confused whether should I go for the replacement of the components which are there inside the container or should I create a new container on every button click and swap the whole container itself.

PS:more concerned about the performance.

regards.

Tirath
  • 2,294
  • 18
  • 27

2 Answers2

1

You can swap the components using a Container method called replace, where you only need to pass the new Component and the old one. You can use an animation too or set it to null.

Mun0n
  • 4,438
  • 4
  • 28
  • 46
  • Thanks for your reply. Let me try again. I want to know which approach is more efficient - like swapping containers having components in it or having a container and swapping only the components which needs to be removed or added to this container. please let me know if we need any more information here. thanks. – Tirath Oct 28 '13 at 10:31
  • 1
    It's more efficient swapping only the components which needs to be removed, because you will destroy these memory references, and the app will be more efficient – Mun0n Oct 28 '13 at 10:58
1

You can add and remove element from the container with addComponent() and removeComponent(). Is is quite similar to an vector of elements.

I wrote about an example for Codename One, where I swap components in a grid: http://meier-online.com/en/2012/10/codename-one-mirror-grid/ This should work the same in lwuit (except perhaps the animation)

I don't think there is much performance difference between changing containers or changing components inside them. For repaint, the layout must be recalculated in both cases, and the container itself has not such a big memory footprint (like for example an image). But you should notice that a Component has a method getParent(), so it is meant to have only one parent. So I would use only one container and swap the elements.

Meier
  • 3,858
  • 1
  • 17
  • 46
  • 1
    This is the actual correct answer, while jmunoz answer with the replace method is indeed correct replacing the whole container will not incur any significant performance penalty and might even be faster since you can perform just one operation instead of several. – Shai Almog Nov 03 '13 at 11:22