PDA

View Full Version : Best practice to switch between components



zaccret
27 May 2008, 5:12 AM
I think this is more Javascript than Java discussion.
I have a set of containers, containing other components. No more than one container is displayed in a Viewport at the same time. I often switch between these containers. You can compare that with Eclipse perspectives.
So in the Viewport, when I replace a component (that I will probably re-use later) by another, what is the best practice regarding performance and memory usage :
- keep the component in a variable, remove it from the Viewport and add the new one
- keep the component (or its id) in a variable, hide it, and add the new one to the Viewport (for that case, using a CardLayout is equivalent, I think)

zaccret
9 Jun 2008, 10:44 PM
I have written a CardLayoutContainer :

public class CardLayoutContainer extends LayoutContainer {

private Set<Component> items = new HashSet<Component>();

public CardLayoutContainer() {
setLayout(new CardLayout());
}

public void switchToComponent(Component cp){
((CardLayout)getLayout()).setActiveItem(cp);
if(add(cp))
layout();
}

@Override
protected boolean add(Component component) {
if(items.add(component)){
super.add(component);
return true;
}
return false;
}

@Override
public boolean removeAll() {
int count = getItemCount();
for (int i = 0; i < count; i++) {
doRemove(getItem(0));
}
return getItemCount() == 0;
}

@Override
public boolean remove(Widget widget) {
return doRemove(widget);
}

private boolean doRemove(Widget widget){
if(super.remove(widget)){
items.remove(widget);
return true;
}
return false;
}
}

gslender
9 Jun 2008, 10:54 PM
this is a good question, and something that I'd like to understand in terms of what happens when you finish with a Window/Dialog - ie in Java the object goes out of scope, does it get destroyed (I don't think it does) so when you open another, am I just using up more and more memory? If so, wouldn't GWT apps suffer from memory leaks (or excessive consumption?)