简体   繁体   English

Vaadin UI类不会添加组件

[英]Vaadin UI class won't add components

I was hoping you could help me figure out why my UI class isn't adding components. 我希望您能帮助我弄清楚为什么我的UI类未添加组件。

What I was hoping for was a bar along the top with 4 buttons each with a link. 我希望的是顶部有一个带有4个按钮的链接栏。 However, when I load a page, nothing shows. 但是,当我加载页面时,没有任何显示。 A blue bar flashes at the top but this quickly fades(I think it's some kind of load bar). 一个蓝色的条形闪烁在顶部,但是很快消失(我认为这是某种载荷条)。 Does anyone have the missing puzzle piece? 有人有缺少的拼图吗?

MyUI.java 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;
    }

}

The answer is I was missing a way to send the view through to the page. 答案是我缺少将视图发送到页面的方法。 To fix this, I did the following: 为了解决这个问题,我做了以下工作:

1, I moved this from the method to the class as a field: 1,我将此作为方法从方法移到类中:

final VerticalLayout myWindow = new VerticalLayout();

2, I made the class MyUI extend UI but also implement ViewDisplay, like this: 2,我使类MyUI扩展了UI,还实现了ViewDisplay,如下所示:

public class MyUI extends UI implements ViewDisplay {}

3, I added the showView method, as required by the ViewDisplay class. 3,根据ViewDisplay类的要求,添加了showView方法。 The single line in there is necessary to the functionality, as far as I can tell: 据我所知,其中的单行功能是必需的:

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

Once these things are done, it should be working. 完成这些操作后,它应该可以正常工作。

To use navigateTo do you need initialize the Navitator class on the UI. 要使用navigateTo你需要初始化UI上Navitator类。 This is the missing puzzle piece. 这是缺少的拼图。 getNavigator() return that instance. getNavigator()返回该实例。 Check the Vaadin reference: https://vaadin.com/docs/-/part/framework/advanced/advanced-navigator.html 检查Vaadin参考: https : //vaadin.com/docs/-//part/framework/advanced/advanced-navigator.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM