You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components-customcomponent.asciidoc 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ---
  2. title: Composition with CustomComponent
  3. order: 31
  4. layout: page
  5. ---
  6. [[components.customcomponent]]
  7. = Composition with CustomComponent
  8. The ease of making new user interface components is one of the core features of
  9. Vaadin. Typically, you simply combine existing built-in components to produce
  10. composite components. In many applications, such composite components make up
  11. the majority of the user interface.
  12. As described earlier in
  13. <<dummy/../../../framework/application/application-architecture#application.architecture.composition,"Compositing
  14. Components">>, you have two basic ways to create a composite - either by
  15. extending a layout component or the [classname]#CustomComponent#, which
  16. typically wraps around a layout component. The benefit of wrapping a layout
  17. composite in [classname]#CustomComponent# is mainly encapsulation - hiding the
  18. implementation details of the composition. Otherwise, a user of the composite
  19. could rely on implementation details, which would create an unwanted dependency.
  20. To create a composite, you need to inherit the [classname]#CustomComponent# and
  21. set the __composition root__ component in the constructor. The composition root
  22. is typically a layout component that contains other components.
  23. For example:
  24. [source, java]
  25. ----
  26. class MyComposite extends CustomComponent {
  27. public MyComposite(String message) {
  28. // A layout structure used for composition
  29. Panel panel = new Panel("My Custom Component");
  30. VerticalLayout panelContent = new VerticalLayout();
  31. panel.setContent(panelContent);
  32. // Compose from multiple components
  33. Label label = new Label(message);
  34. panelContent.addComponent(label);
  35. panelContent.addComponent(new Button("Ok"));
  36. // Set the size as undefined at all levels
  37. panelContent.setSizeUndefined();
  38. panel.setSizeUndefined();
  39. setSizeUndefined();
  40. // The composition root MUST be set
  41. setCompositionRoot(panel);
  42. }
  43. }
  44. ----
  45. Take note of the sizing when trying to make a customcomponent that shrinks to
  46. fit the contained components. You have to set the size as undefined at all
  47. levels; the sizing of the composite component and the composition root are
  48. separate.
  49. You can use the component as follows:
  50. [source, java]
  51. ----
  52. MyComposite mycomposite = new MyComposite("Hello");
  53. ----
  54. The rendered component is shown in <<figure.components.customcomponent>>.
  55. [[figure.components.customcomponent]]
  56. .A custom composite component
  57. image::img/customcomponent-example1.png[width=25%, scaledwidth=40%]
  58. You can also inherit any other components, such as layouts, to attain similar
  59. composition.
  60. ((("Google Web Toolkit")))
  61. Even further, you can create entirely new low-level components, by integrating
  62. pure client-side components or by extending the client-side functionality of
  63. built-in components. Development of new components is covered in
  64. <<dummy/../../../framework/gwt/gwt-overview.asciidoc#gwt.overview,"Integrating
  65. with the Server-Side">>.