This is probably a difficult one… sorry.
I’m trying to remove a component from the main window to move it to a modal window. Then I move it back to the main window.
I’ve an example that works and another that fails.
Basically, when moving from the main window to the modal:
- instantiate new Window (modal)
- mainWindow.removeComponent
- modal window .setContent of my component
When moving back from the modal to the main, I do:
- modal window .setContent(null)
- mainWindow.addComponent( my component )
It works fine, except when I change the last step:
If I put the component inside a Layout (itself inside the mainWindow), then the component does not come back to the main Window.
So, subLayout.addComponent( my component) does not work, while mainWindow.addComponent( my component ) works.
I’ve no hell idea why
Does somebody know?
Here is the full code if you want to reproduce: the first case works, the second is very similar but does not work.
package com.example.vaadintest2;
import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
public class Vaadintest2Application extends Application {
Window modalWindow = null;
Window modalWindow2 = null;
@Override
public void init() {
final Window mainWindow = new Window("Vaadintest2 Application");
Label label = new Label("Hello Vaadin user");
mainWindow.addComponent(label);
setMainWindow(mainWindow);
// 1111111111111
// This can move forth and back.
final HorizontalLayout movingContainer = new HorizontalLayout();
movingContainer.addComponent(new Label("I can move to a form and back"));
movingContainer.addComponent(new Button("click me", new ClickListener() {
public void buttonClick(ClickEvent event) {
if (modalWindow == null) { // label not yet in modal window
modalWindow = new Window();
modalWindow.setModal(true);
mainWindow.removeComponent(movingContainer);
modalWindow.setContent(movingContainer);
mainWindow.addWindow(modalWindow); // DIFFERENCE
} else { // label already in modal window => we close.
modalWindow.setContent(null);
mainWindow.removeWindow(modalWindow);
modalWindow = null;
mainWindow.addComponent(movingContainer); // DIFFERENCE
}
}
}));
mainWindow.addComponent(movingContainer); // DIFFERENCE
// 22222222222
final VerticalLayout vLayout = new VerticalLayout();
mainWindow.addComponent(vLayout);
// This can move to the form but can not come back to the main window.
final HorizontalLayout movingContainer2 = new HorizontalLayout();
movingContainer2.addComponent(new Label("I can move to a form but NOT back"));
movingContainer2.addComponent(new Button("click me 2", new ClickListener() {
public void buttonClick(ClickEvent event) {
if (modalWindow2 == null) { // label not yet in modal window
modalWindow2 = new Window();
modalWindow2.setModal(true);
vLayout.removeComponent(movingContainer2); // DIFFERENCE
modalWindow2.setContent(movingContainer2);
mainWindow.addWindow(modalWindow2);
} else { // label already in modal window => we close.
modalWindow2.setContent(null);
mainWindow.removeWindow(modalWindow2);
modalWindow2 = null;
vLayout.addComponent(movingContainer2); // DIFFERENCE
}
}
}));
vLayout.addComponent(movingContainer2); // DIFFERENCE
}
}