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.3KB

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