`
lyunabc
  • 浏览: 526052 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论

Vaadin Web应用开发教程(27):UI组件-自定义组件

 
阅读更多

Vaadin 支持自定义组件,典型的用法是将各种Vaadin内置的组件组合而成构成自定义组件。 创建自定义组件可以通过派生CustomComponent 然后调用setCompositionRoot 为自定义组件设置根容器。
例如:

class MyComposite extends CustomComponent {
    public MyComposite(String message) {
        // A layout structure used for composition
        Panel panel = new Panel("My Custom Component");
        panel.setContent(new VerticalLayout());
        
        // Compose from multiple components
        Label label = new Label(message);
        label.setSizeUndefined(); // Shrink
        panel.addComponent(label);
        panel.addComponent(new Button("Ok"));

        // Set the size as undefined at all levels
        panel.getContent().setSizeUndefined();
        panel.setSizeUndefined();
        setSizeUndefined();

        // The composition root MUST be set
        setCompositionRoot(panel);
    }}



需要注意的是,如果希望自定义的组件自适应其所包含的其它UI组件,必须将容器的大小设为“未定义”,如上面的setSizeUndefined方法就是起这个作用。
构造自定义组件,也可以从其它Vaadin内置UI组件派生,或者利用Google Web Toolbit提供的组件创建全新的Vaadin UI组件(后面介绍)。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics