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 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ---
  2. title: Composition with CustomComponent
  3. order: 31
  4. layout: page
  5. ---
  6. [[components.customcomponent]]
  7. = Composition with [classname]#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. panelContent.setMargin(true); // Very useful
  32. panel.setContent(panelContent);
  33. // Compose from multiple components
  34. Label label = new Label(message);
  35. label.setSizeUndefined(); // Shrink
  36. panelContent.addComponent(label);
  37. panelContent.addComponent(new Button("Ok"));
  38. // Set the size as undefined at all levels
  39. panelContent.setSizeUndefined();
  40. panel.setSizeUndefined();
  41. setSizeUndefined();
  42. // The composition root MUST be set
  43. setCompositionRoot(panel);
  44. }
  45. }
  46. ----
  47. Take note of the sizing when trying to make a customcomponent that shrinks to
  48. fit the contained components. You have to set the size as undefined at all
  49. levels; the sizing of the composite component and the composition root are
  50. separate.
  51. You can use the component as follows:
  52. [source, java]
  53. ----
  54. MyComposite mycomposite = new MyComposite("Hello");
  55. ----
  56. The rendered component is shown in <<figure.components.customcomponent>>.
  57. [[figure.components.customcomponent]]
  58. .A custom composite component
  59. image::img/customcomponent-example1.png[width=25%, scaledwidth=40%]
  60. You can also inherit any other components, such as layouts, to attain similar
  61. composition.
  62. ((("Google Web Toolkit")))
  63. Even further, you can create entirely new low-level components, by integrating
  64. pure client-side components or by extending the client-side functionality of
  65. built-in components. Development of new components is covered in
  66. <<dummy/../../../framework/gwt/gwt-overview.asciidoc#gwt.overview,"Integrating
  67. with the Server-Side">>.