Java 无法增加Vaadin 8中栅格的宽度

Java 无法增加Vaadin 8中栅格的宽度,java,vaadin,vaadin7,vaadin8,Java,Vaadin,Vaadin7,Vaadin8,public MyTabbedForm(){ this.refresh=新建按钮(“”); this.refresh.addClickListener(按钮ClickEvent->{ this.grid.setItems(getCompany()); }); this.grid=新网格(); this.grid.setWidthFull(); this.grid.addColumn(Company::getName).setHeader(“Name”); this.grid.addColumn(

public MyTabbedForm(){
this.refresh=新建按钮(“”);
this.refresh.addClickListener(按钮ClickEvent->{
this.grid.setItems(getCompany());
});
this.grid=新网格();
this.grid.setWidthFull();
this.grid.addColumn(Company::getName).setHeader(“Name”);
this.grid.addColumn(company->(company.getCompanyCode()))
.setHeader(“公司代码”);
this.grid.addColumn(company->(company.getCompanyId())
.setHeader(“公司Id”);
this.grid.addColumn(company->(company.getCompanyStatus()))
.setHeader(“公司状态”);
this.grid.getColumns().forEach(col->col.setAutoWidth(true));
添加(this.refresh,this.grid);
}

但是我可以根据内容增加列的宽度

您正在将
网格
设置为全宽。在Vaadin 8中,这意味着
网格
应占据布局中插槽的全部大小。假设您在
垂直布局中有一个宽度为“100px”的网格,并且您将
网格设置为全宽,它将是“100px”宽。我看到你的
网格
在某个父布局中,可能在某个父布局中,等等。你需要遵循这个链,在那里也设置了正确的宽度


另请查看培训视频。

在MyTabbedForm构造函数的末尾,尝试添加以下行

public MyTabbedForm() {
        this.refresh = new Button("");
        this.refresh.addClickListener(buttonClickEvent -> {
            this.grid.setItems(getCompany());
   
        });
        this.grid = new Grid<>();
        this.grid.setWidthFull();
        this.grid.addColumn(Company::getName).setHeader("Name");
        this.grid.addColumn(company -> (company. getCompanyCode()))
                .setHeader("Company Code");
        this.grid.addColumn(company -> (company. getCompanyId()))
        .setHeader("Company Id");
        this.grid.addColumn(company -> (company. getCompanyStatus()))
        .setHeader("Company Status");
        this.grid.getColumns().forEach(col -> col.setAutoWidth(true));
        add(this.refresh,this.grid);
    }
这将为栅格的所有祖先设置100%的宽度

    Component parent = this.grid.getParent();
    while (null != parent) {
        if (parent instanceof AbstractComponent) {
            parent.setWidthFull();
        }
        parent = getParent();
    }