以编程方式调整大小/重新定位vaadin窗口

时间:2013-10-30 16:12:58

标签: vaadin7

我们有一个Vaadin 7应用程序,可以有几个Vaadin窗口对象。为了帮助用户管理这些窗口,我想要一个按钮来调整窗口的大小/重新定位,例如平铺窗口。我的代码是这样的:

for (Window window : ui.getWindows()) {
    window.setHeight(...);
    window.setWidth(...);
    window.setPositionX(...);
    window.setPositionY(....);
}
  1. 窗口被添加到UI,用户与之交互,按下运行上述伪代码的按钮,窗口响应。 (良好)。

  2. 如果用户现在再次移动/调整窗口大小,则相同的按钮无效。 (坏)

  3. 任何想法为什么调整大小/重新定位不会继续在上面的情况2中起作用?

    我创建了一个测试程序来澄清我的问题:

    package com.example.tilewindow;
    
    import java.util.ArrayList;
    import java.util.Collection;
    
    import javax.servlet.annotation.WebServlet;
    
    import com.vaadin.annotations.Theme;
    import com.vaadin.annotations.VaadinServletConfiguration;
    import com.vaadin.server.VaadinRequest;
    import com.vaadin.server.VaadinServlet;
    import com.vaadin.ui.Button;
    import com.vaadin.ui.Button.ClickEvent;
    import com.vaadin.ui.Label;
    import com.vaadin.ui.TextField;
    import com.vaadin.ui.UI;
    import com.vaadin.ui.VerticalLayout;
    import com.vaadin.ui.Window;
    
    @SuppressWarnings("serial")
    @Theme("tilewindow")
    public class TilewindowUI extends UI {
    
    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = TilewindowUI.class)
    public static class Servlet extends VaadinServlet {
    }
    static int counter = 1;
    
    
    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        setContent(layout);
        addWindow(createWindow(0, 0, 300, 300));
        addWindow(createWindow(500, 0, 300, 300));
        addWindow(createWindow(0, 500, 300, 300));
    
        Button newWindowButton = new Button("New Window 2");
        newWindowButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                addWindow(createWindow(0, 0, 500, 500));
            }
        });
        Button tileWindowsButton = new Button("Tile");
        tileWindowsButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                Collection<Window> temp = getWindows();
                Collection<Window> windows = new ArrayList<Window>(temp);
                System.out.println("windows count = " + windows.size());
                int offset = 20;
                for (Window window : windows) {
                    window.setHeight(200, Unit.PIXELS);
                    window.setWidth(200, Unit.PIXELS);
                    window.setPositionX(offset);
                    window.setPositionY(offset);
                    offset += 20;
                    window.markAsDirty();
                    System.out.println("   Found window, offset = ." + offset);
                }
                windows.clear();
                markAsDirty();
            }
        });
        layout.addComponent(newWindowButton);
        layout.addComponent(tileWindowsButton);
    }
    private Window createWindow(int xIn, int yIn, int heightIn, int widthIn) {
        Window returnValue = new Window();
        returnValue.setPositionX(xIn);
        returnValue.setPositionY(yIn);
        returnValue.setHeight(heightIn, Unit.PIXELS);
        returnValue.setWidth(widthIn, Unit.PIXELS);
        VerticalLayout verticalLayout = new VerticalLayout();
        verticalLayout.addComponent(new Label("Window #" + counter++));
        verticalLayout.addComponent(new TextField());
        returnValue.setContent(verticalLayout);
        return (returnValue);
    }
    
    }
    

1 个答案:

答案 0 :(得分:1)

vaadin承认这是一个错误。将在未来版本中修复。

http://dev.vaadin.com/ticket/12885