Best practice for hiding/showing components

Hello Vaadin-Users,

what is the right way for hiding/showing of components in layouts?

Should I use or avoid one of the following ways?

  1. setting the visibility

[code]
final VerticalLayout layout = new VerticalLayout();

Label label = new Label(“Hello World”);
layout.addComponent(label);

Button toggleLabelVisibility = new Button(“toggle label”);
toggleLabelVisibility.addClickListener(new ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {
             if(label.isVisible(){
                 label.setVisible(false);
             } else {
                 label.setVisible(true);
             }
        }
    });

[/code]2. Hiding components with CSS

final VerticalLayout layout = new VerticalLayout();

Label label = new Label("Hello World");
label.addStyleName("invisible");
layout.addComponent(label);

Button toggleLabelVisibility = new Button("toggle label");
toggleLabelVisibility.addClickListener(new ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                 if(label.getStyleName().contains("invisible")){
                     label.removeStyleName("invisible");
                 } else {
                     label.addStyleName("invisible");
                 }
            }
        });


.v-label-invisible{
    display: none;
}
  1. removing/adding components
final VerticalLayout layout = new VerticalLayout();

Label label = new Label("Hello World");
layout.addComponent(label);

Button toggleLabelVisibility = new Button("toggle label");
toggleLabelVisibility.addClickListener(new ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                 if(layout.getComponentCount > 0){
                     layout.removeAllComponents();
                 } else {
                     layout.addComponent(label);
                 }
            }
        });

Thanks in advance!

Johannes