Vaadin UI类无法添加组件

时间:2017-06-13 02:09:46

标签: java spring intellij-idea vaadin

我希望你能帮我弄清楚为什么我的UI类没有添加组件。

我所希望的是一个顶部的条形图,有4个按钮,每个按钮都有一个链接。但是,当我加载页面时,没有任何显示。顶部的蓝色条闪烁,但很快就会消失(我认为它是某种负载条)。有没有人有丢失的拼图?

MyUI.java

package org.vaadin.spring.tutorial;

import com.vaadin.annotations.Theme;
import com.vaadin.navigator.Navigator;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewDisplay;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.spring.annotation.SpringViewDisplay;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ValoTheme;
import org.vaadin.spring.tutorial.views.DefaultView;
import org.vaadin.spring.tutorial.views.LoginView;
import org.vaadin.spring.tutorial.views.UIScopedView;
import org.vaadin.spring.tutorial.views.ViewScopedView;

import javax.swing.text.html.CSS;

@Theme("valo")
@SpringUI
@SpringViewDisplay
public class MyUI extends UI {
    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout myWindow = new VerticalLayout();
        myWindow.setSizeFull();
        setContent(myWindow);

        final CssLayout topBar = new CssLayout();

        for (String[] s: new String[][]{{"UI","ui"}, {"View","view"}, {"","Home"}, {"Login","login"}}) {
            topBar.addComponent(this.createNavigationButton(s[0], s[1]));
        }

        myWindow.addComponent(topBar);
    }

    private Button createNavigationButton(String caption,
                                          final String viewName) {
        Button button = new Button(caption);
        button.addStyleName(ValoTheme.BUTTON_SMALL);
        // If you didn't choose Java 8 when creating the project, convert this
        // to an anonymous listener class
        button.addClickListener(
                event -> getUI().getNavigator().navigateTo(viewName));
        return button;
    }

}

2 个答案:

答案 0 :(得分:0)

答案是我错过了将视图发送到页面的方法。为了解决这个问题,我做了以下几点:

1,我把这个从方法移到了一个字段:

final VerticalLayout myWindow = new VerticalLayout();

2,我创建了MyUI扩展UI类,但也实现了ViewDisplay,如下所示:

public class MyUI extends UI implements ViewDisplay {}

3,我根据ViewDisplay类的要求添加了showView方法。据我所知,那里的单行是功能所必需的:

public void showView(View view) {
    MY_WINDOW.addComponent((Component) view);
}

完成这些工作后,它应该正常工作。

答案 1 :(得分:0)

要使用navigateTo,您需要在UI上初始化Navitator类。这是缺失的拼图。 getNavigator()返回该实例。 检查Vaadin参考:https://vaadin.com/docs/-/part/framework/advanced/advanced-navigator.html