Vaadin,通过setContent()切换内容不显示

时间:2016-03-11 19:39:06

标签: vaadin vaadin7

Vaadin 7.6.2

采用以下示例:

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class MyClass extends Panel {

    TextField        myField  = new TextField();
    HorizontalLayout hLayout  = new HorizontalLayout( myField );
    VerticalLayout   vLayout  = new VerticalLayout( myField );    
    Button           button   = new Button( "Press Me" );

    public MyClass() {
        super();
        applySettings();
    }

    private void applySettings() {
        button.addClickListener(new ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                setContent( hLayout );
            }
        });

        vLayout.addComponent( button );


        this.setContent( vLayout );
    }
}

当我点击按钮时, vLayout 消失,但 hLayout (带有myField)不会显示。我错过了什么步骤?或者,有不同的方法吗?

如果我添加辅助文本字段,请执行以下操作:

TextField        myField  = new TextField();
TextField        myField2 = new TextField();  // tf2
HorizontalLayout hLayout  = new HorizontalLayout( myField );
VerticalLayout   vLayout  = new VerticalLayout( myField2 );  // tf2

它似乎有效,但我想要实现的是能够使用切出布局中的字段(及其数据)动态切换布局。

2 个答案:

答案 0 :(得分:1)

好吧,看起来我可能已经解决了它。

selected_data <- reactive({

query <- dbSendQuery(conn = con, statement = paste('SELECT * FROM pitch_velo WHERE pitcher_name = ', "\"",input$pitchername,"\"",';',sep=''))
pitch_data <- dbFetch(query,n=-1)

minyear <- input$year[1]
maxyear <- input$year[2]

velo <- pitch_data %>% group_by(pitcher_name, pitch_type, Year) %>% summarise(velocity = mean(start_speed)) %>% arrange(velocity) %>% dplyr::filter(pitcher_name %in% input$pitcher_name) %>% dplyr::arrange(pitcher_name) %>% dplyr::filter(Year >= minyear) %>% dplyr::filter(Year <= maxyear) %>% dplyr::filter(pitch_type %in% input$pitch_type)
print(velo)

如果我不在布局构造函数中添加import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Panel; import com.vaadin.ui.TextField; import com.vaadin.ui.VerticalLayout; public class MyClass extends Panel { TextField myField = new TextField(); HorizontalLayout hLayout = new HorizontalLayout(); VerticalLayout vLayout = new VerticalLayout(); Button button = new Button( "Press Me" ); public MyClass() { super(); applySettings(); } private void applySettings() { button.addClickListener(new ClickListener() { @Override public void buttonClick(Button.ClickEvent event) { vLayout.removeAllComponents(); // this is optional hLayout.addComponent( myField ); setContent( hLayout ); } }); vLayout.addComponents( myField, button ); this.setContent( vLayout ); } } ,但稍后在代码中添加它,它似乎可以工作。

<强>更新

经过更多测试后,对于给定字段,无论哪种布局LAST称为myField,该布局都会获得该字段的句柄。这种行为似乎很奇怪,如果有人能解释为什么会这样?那会很有启发性。

答案 1 :(得分:1)

一个组件不能同时拥有2个父项(在您的情况下为hLayout&amp; vLayout),因此如果它已经有一个,则Vaadin会将其从前一个父项中删除并添加它作为现在的孩子。这是从AbstractComponentContainer继承的addComponent方法:

/**
 * This only implements the events and component parent calls. The extending
 * classes must implement component list maintenance and call this method
 * after component list maintenance.
 * 
 * @see com.vaadin.ui.ComponentContainer#addComponent(Component)
 */
@Override
public void addComponent(Component c) {
    // Make sure we're not adding the component inside it's own content
    if (isOrHasAncestor(c)) {
        throw new IllegalArgumentException(
                "Component cannot be added inside it's own content");
    }

    if (c.getParent() != null) {
        // If the component already has a parent, try to remove it
        AbstractSingleComponentContainer.removeFromParent(c);
    }

    c.setParent(this);
    fireComponentAttachEvent(c);
    markAsDirty();
}

如果您在debug mode,则可以在浏览器中看到合成树的图片,方法是在您的网址中添加?debug,例如http://localhost:8080/?debug

component hierarchy