Vaadin 支持自定義組件,典型的用法是將各種 Vaadin 內(nèi)置的組件組合而成構(gòu)成自定義組件。 創(chuàng)建自定義組件可以通過派生 CustomComponent 然后調(diào)用 setCompositionRoot 為自定義組件設(shè)置根容器。 例如:
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);
}}
http://wiki.jikexueyuan.com/project/vaadin-web-development-tutorial/images/70.png" alt="" />
需要注意的是,如果希望自定義的組件自適應(yīng)其所包含的其它UI組件,必須將容器的大小設(shè)為“未定義”,如上面的 setSizeUndefined 方法就是起這個(gè)作用。 構(gòu)造自定義組件,也可以從其它 Vaadin 內(nèi)置 UI 組件派生,或者利用 Google Web Toolbit 提供的組件創(chuàng)建全新的 Vaadin UI 組件(后面介紹)。