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.

CreatingASimpleComponent.asciidoc 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ---
  2. title: Creating A Simple Component
  3. order: 32
  4. layout: page
  5. ---
  6. [[creating-a-simple-component]]
  7. = Creating a simple component
  8. To make a component with a new client-side widget (as opposed to making
  9. a server-side composite), you will need to make three things: the
  10. _server-side component_ you'll actually use in your application (let's
  11. call it *MyComponent*), the corresponding _client-side (GWT) widget_
  12. that will render your component in the browser (*MyComponentWidget*) and
  13. a _Connector_ that handles the communication between the two
  14. (*MyComponentConnector*). (Note that although MyComponentWidget could in
  15. principle be a Connector as well, in practice it's a good idea to
  16. separate the two.)
  17. At this point the basic MyComponent has no functionality except
  18. inherited basic component features (we'll add functionality in following
  19. articles):
  20. [source,java]
  21. ....
  22. package com.example.mycomponent;
  23. import com.vaadin.ui.AbstractComponent;
  24. public class MyComponent extends AbstractComponent {
  25. }
  26. ....
  27. The main thing to notice here is that it inherits `AbstractComponent`,
  28. which is the most common case (unless it will contain other components,
  29. see separate article about component containers). The component will
  30. automatically have the basic component features, such as size and
  31. caption.
  32. At this point our basic client-side widget will just statically render
  33. some text:
  34. [source,java]
  35. ....
  36. package com.example.mycomponent.client;
  37. import com.google.gwt.user.client.ui.Label;
  38. public class MyComponentWidget extends Label {
  39. public static final String CLASSNAME = "mycomponent";
  40. public MyComponentWidget() {
  41. setText("This is MyComponent");
  42. setStyleName(CLASSNAME);
  43. }
  44. }
  45. ....
  46. Notice that this is actually a plain GWT widget that can be used as any
  47. other GWT widget. It's a good idea to set a style name from the start,
  48. so that the component can be styled.
  49. Now all we have to do is connect the component to the widget using a
  50. Connector:
  51. [source,java]
  52. ....
  53. package com.example.mycomponent.client;
  54. import com.example.mycomponent.MyComponent;
  55. import com.google.gwt.core.client.GWT;
  56. import com.google.gwt.user.client.ui.Widget;
  57. import com.vaadin.client.ui.AbstractComponentConnector;
  58. import com.vaadin.client.ui.Connect;
  59. @Connect(com.example.mycomponent.MyComponent.class)
  60. public class MyComponentConnector extends AbstractComponentConnector {
  61. @Override
  62. protected Widget createWidget() {
  63. return GWT.create(MyComponentWidget.class);
  64. }
  65. }
  66. ....
  67. The *crucial Connect annotation* is what actually tells the framework
  68. what is connected where - do this first, since it's easy to forget.
  69. In `createWidget()` use `GWT.create()` instead of `new` whenever possible,
  70. since it allows for some flexibility that might come in handy later on.
  71. Though this is optional, you might also want to override getWidget() so
  72. that you can narrow it's return type from Widget to your actual
  73. implementation class:
  74. [source,java]
  75. ....
  76. @Override
  77. public MyComponentWidget getWidget() {
  78. return (MyComponentWidget) super.getWidget();
  79. }
  80. ....
  81. The package structure usually looks something like this:
  82. * com.example.mycomponent
  83. ** MyComponent.java
  84. ** MyComponentWidgetset.gwt.xml
  85. * com.example.mycomponent.client
  86. ** MyComponentConnector.java
  87. ** MyComponentWidget.java
  88. Finally, compile the widgetset, and *make sure web.xml contains the
  89. widgetset parameter:*
  90. [source,xml]
  91. ....
  92. <servlet>
  93. <servlet-name>My Vaadin App</servlet-name>
  94. <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
  95. <init-param>
  96. <description>Vaadin UI</description>
  97. <param-name>UI</param-name>
  98. <param-value>com.example.myexampleproject.MyApplicationUI</param-value>
  99. </init-param>
  100. <init-param>
  101. <param-name>widgetset</param-name>
  102. <param-value>com.example.mycomponent.MyComponentWidgetset</param-value>
  103. </init-param>
  104. </servlet>
  105. ....
  106. Add MyComponent to your application, and it should render a label saying
  107. "This is MyComponent".
  108. Next have a look at the articles covering shared state and RPC, to learn
  109. how to add more functionality to the component.