Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

components-features.asciidoc 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. ---
  2. title: Common Component Features
  3. order: 3
  4. layout: page
  5. ---
  6. [[components.features]]
  7. = Common Component Features
  8. The component base classes and interfaces provide a large number of features.
  9. Let us look at some of the most commonly needed features. Features not
  10. documented here can be found from the Java API Reference.
  11. The interface defines a number of properties, which you can retrieve or
  12. manipulate with the corresponding setters and getters.
  13. [[components.features.caption]]
  14. == Caption
  15. ((("caption property")))
  16. ((("Component interface", "caption")))
  17. A caption is an explanatory textual label accompanying a user interface
  18. component, usually shown above, left of, or inside the component. The contents
  19. of a caption are automatically quoted, so no raw HTML can be rendered in a
  20. caption.
  21. The caption text can usually be given as the first parameter of a constructor of
  22. a component or with [methodname]#setCaption()#.
  23. [source, java]
  24. ----
  25. // New text field with caption "Name"
  26. TextField name = new TextField("Name");
  27. layout.addComponent(name);
  28. ----
  29. The caption of a component is, by default, managed and displayed by the layout
  30. component or component container inside which the component is placed. For
  31. example, the [classname]#VerticalLayout# component shows the captions
  32. left-aligned above the contained components, while the [classname]#FormLayout#
  33. component shows the captions on the left side of the vertically laid components,
  34. with the captions and their associated components left-aligned in their own
  35. columns. The [classname]#CustomComponent# does not manage the caption of its
  36. composition root, so if the root component has a caption, it will not be
  37. rendered.
  38. [[figure.components.features.caption.layoutmanaged]]
  39. .Caption Management by [classname]#VerticalLayout# and [classname]#FormLayout#.
  40. image::img/features-caption-layoutmanaged.png[width=50%,scaledwidth=65%]
  41. Some components, such as [classname]#Button# and [classname]#Panel#, manage the
  42. caption themselves and display it inside the component.
  43. Icon (see <<components.features.icon>>) is closely related to caption and is
  44. usually displayed horizontally before or after it, depending on the component
  45. and the containing layout. Also the required indicator in field components is
  46. usually shown before or after the caption.
  47. An alternative way to implement a caption is to use another component as the
  48. caption, typically a [classname]#Label#, a [classname]#TextField#, or a
  49. [classname]#Panel#. A [classname]#Label#, for example, allows highlighting a
  50. shortcut key with HTML markup or to bind the caption to a data source. The
  51. [classname]#Panel# provides an easy way to add both a caption and a border
  52. around a component.
  53. === CSS Style Rules
  54. [source, css]
  55. ----
  56. .v-caption {}
  57. .v-captiontext {}
  58. .v-required-field-indicator {}
  59. ----
  60. A caption is be rendered inside an HTML element that has the
  61. [literal]#++v-caption++# CSS style class. The containing layout may enclose a
  62. caption inside other caption-related elements.
  63. Some layouts put the caption text in a [literal]#++v-captiontext++# element.
  64. An optional required indicator in field components is contained in a separate element with
  65. [literal]#++v-required-field-indicator++# style.
  66. [[components.features.description]]
  67. == Description and Tooltips
  68. ((("description property")))
  69. ((("Component interface", "description")))
  70. ((("tooltips")))
  71. All components (that inherit [classname]#AbstractComponent#) have a description
  72. separate from their caption. The description is usually shown as a tooltip that
  73. appears when the mouse pointer hovers over the component for a short time.
  74. You can set the description with [methodname]#setDescription()# and retrieve
  75. with [methodname]#getDescription()#.
  76. [source, java]
  77. ----
  78. Button button = new Button("A Button");
  79. button.setDescription("This is the tooltip");
  80. ----
  81. The tooltip is shown in <<figure.components.tooltip.plain>>.
  82. [[figure.components.tooltip.plain]]
  83. .Component Description as a Tooltip
  84. image::img/tooltip-plain-withpointer-hi.png[width=30%, scaledwidth=100%]
  85. A description is rendered as a tooltip in most components.
  86. When a component error has been set with [methodname]#setComponentError()#, the
  87. error is usually also displayed in the tooltip, below the description.
  88. Components that are in error state will also display the error indicator. See
  89. <<../application/application-errors#application.errors.error-indicator, "Error Indicator and Message">>.
  90. The description is actually not plain text, but you can use HTML tags to format
  91. it. Such a rich text description can contain any HTML elements, including
  92. images.
  93. [source, java]
  94. ----
  95. button.setDescription(
  96. "<h2><img src=\"../VAADIN/themes/sampler/"+
  97. "icons/comment_yellow.gif\"/>"+
  98. "A richtext tooltip</h2>"+
  99. "<ul>"+
  100. " <li>Use rich formatting with HTML</li>"+
  101. " <li>Include images from themes</li>"+
  102. " <li>etc.</li>"+
  103. "</ul>");
  104. ----
  105. The result is shown in <<figure.components.tooltip.richtext>>.
  106. [[figure.components.tooltip.richtext]]
  107. .A Rich Text Tooltip
  108. image::img/tooltip-richtext-withpointer-hi.png[width=40%, scaledwidth=75%]
  109. [[components.features.enabled]]
  110. == Enabled
  111. ((("enabled property")))
  112. ((("Component interface", "enabled")))
  113. The __enabled__ property controls whether the user can actually use the
  114. component. A disabled component is visible, but grayed to indicate the disabled
  115. state.
  116. Components are always enabled by default. You can disable a component with
  117. [methodname]#setEnabled(false)#.
  118. [source, java]
  119. ----
  120. Button enabled = new Button("Enabled");
  121. enabled.setEnabled(true); // The default
  122. layout.addComponent(enabled);
  123. Button disabled = new Button("Disabled");
  124. disabled.setEnabled(false);
  125. layout.addComponent(disabled);
  126. ----
  127. <<figure.components.features.enabled.simple>> shows the enabled and disabled
  128. buttons.
  129. [[figure.components.features.enabled.simple]]
  130. .An Enabled and Disabled [classname]#Button#
  131. image::img/features-enabled-simple.png[width=30%, scaledwidth=50%]
  132. A disabled component is automatically put in read-only like state. No client
  133. interaction with a disabled component is sent to the server and, as an important
  134. security feature, the server-side components do not receive state updates from
  135. the client in the disabled state. This feature exists in all built-in
  136. components in the Framework meaning all client to server RPC calls are ignored
  137. for disabled components.
  138. === CSS Style Rules
  139. Disabled components have the [literal]#++v-disabled++# CSS style in addition to
  140. the component-specific style. To match a component with both the styles, you
  141. have to join the style class names with a dot as done in the example below.
  142. [source, css]
  143. ----
  144. .v-textfield.v-disabled {
  145. border: dotted;
  146. }
  147. ----
  148. This would make the border of all disabled text fields dotted.
  149. In the Valo theme, the opacity of disabled components is specified with the
  150. `$v-disabled-opacity` parameter.
  151. [[components.features.icon]]
  152. == Icon
  153. ((("icon property")))
  154. ((("Component interface", "icon")))
  155. An icon is an explanatory graphical label accompanying a user interface
  156. component, usually shown above, left of, or inside the component. Icon is
  157. closely related to caption (see <<components.features.caption>>) and is usually
  158. displayed horizontally before or after it, depending on the component and the
  159. containing layout.
  160. The icon of a component can be set with the [methodname]#setIcon()# method. The
  161. image is provided as a resource, perhaps most typically a
  162. [classname]#ThemeResource#.
  163. [source, java]
  164. ----
  165. // Component with an icon from a custom theme
  166. TextField name = new TextField("Name");
  167. name.setIcon(new ThemeResource("icons/user.png"));
  168. layout.addComponent(name);
  169. // Component with an icon from another theme ('runo')
  170. Button ok = new Button("OK");
  171. ok.setIcon(new ThemeResource("../runo/icons/16/ok.png"));
  172. layout.addComponent(ok);
  173. ----
  174. The icon of a component is, by default, managed and displayed by the layout
  175. component or component container in which the component is placed. For example,
  176. the [classname]#VerticalLayout# component shows the icons left-aligned above the
  177. contained components, while the [classname]#FormLayout# component shows the
  178. icons on the left side of the vertically laid components, with the icons and
  179. their associated components left-aligned in their own columns. The
  180. [classname]#CustomComponent# does not manage the icon of its composition root,
  181. so if the root component has an icon, it will not be rendered.
  182. [[figure.components.features.icon]]
  183. .Displaying an Icon from a Theme Resource.
  184. image::img/features-icon.png[width=40%, scaledwidth=60%]
  185. Some components, such as [classname]#Button# and [classname]#Panel#, manage the
  186. icon themselves and display it inside the component.
  187. In addition to image resources, you can use __font icons__, which are icons
  188. included in special fonts, but which are handled as special resources. See
  189. <<../themes/themes-fonticon#themes.fonticon,"Font Icons">>
  190. for more details.
  191. === CSS Style Rules
  192. An icon will be rendered inside an HTML element that has the
  193. [literal]#++v-icon++# CSS style class. The containing layout may enclose an icon
  194. and a caption inside elements related to the caption, such as
  195. [literal]#++v-caption++#.
  196. [[components.features.locale]]
  197. == Locale
  198. ((("locale property", "in [classname]#Component#")))
  199. ((("Component interface", "locale")))
  200. The locale property defines the country and language used in a component. You
  201. can use the locale information in conjunction with an internationalization
  202. scheme to acquire localized resources. Some components, such as
  203. [classname]#DateField#, use the locale for component localization.
  204. You can set the locale of a component (or the application) with
  205. [methodname]#setLocale()# as follows:
  206. [source, java]
  207. ----
  208. // Component for which the locale is meaningful
  209. InlineDateField date = new InlineDateField("Datum");
  210. // German language specified with ISO 639-1 language
  211. // code and ISO 3166-1 alpha-2 country code.
  212. date.setLocale(new Locale("de", "DE"));
  213. date.setResolution(Resolution.DAY);
  214. layout.addComponent(date);
  215. ----
  216. The resulting date field is shown in <<figure.components.features.locale.simple>>.
  217. [[figure.components.features.locale.simple]]
  218. .Set locale for [classname]#InlineDateField#
  219. image::img/features-locale-simple.png[width=40%, scaledwidth=60%]
  220. [[components.features.locale.get]]
  221. === Getting the Locale
  222. ((("[methodname]#getLocale()#")))
  223. You can get the locale of a component with [methodname]#getLocale()#. If the
  224. locale is undefined for a component, that is, not explicitly set, the locale of
  225. the parent component is used. If none of the parent components have a locale
  226. set, the locale of the UI is used, and if that is not set, the default system
  227. locale is set, as given by [methodname]#Locale.getDefault()#.
  228. The [methodname]#getLocale()# returns null if the component is not yet attached
  229. to the UI, which is usually the case in most constructors, so it is a bit
  230. awkward to use it for internationalization. You can get the locale in
  231. [methodname]#attach()#, as shown in the following example:
  232. [source, java]
  233. ----
  234. Button cancel = new Button() {
  235. @Override
  236. public void attach() {
  237. super.attach();
  238. ResourceBundle bundle = ResourceBundle.getBundle(
  239. MyAppCaptions.class.getName(), getLocale());
  240. setCaption(bundle.getString(MyAppCaptions.CancelKey));
  241. }
  242. };
  243. layout.addComponent(cancel);
  244. ----
  245. However, it is normally a better practice to use the locale of the current UI to
  246. get the localized resource right when the component is created.
  247. [source, java]
  248. ----
  249. // Captions are stored in MyAppCaptions resource bundle
  250. // and the UI object is known in this context.
  251. ResourceBundle bundle =
  252. ResourceBundle.getBundle(MyAppCaptions.class.getName(),
  253. UI.getCurrent().getLocale());
  254. // Get a localized resource from the bundle
  255. Button cancel =
  256. new Button(bundle.getString(MyAppCaptions.CancelKey));
  257. layout.addComponent(cancel);
  258. ----
  259. [[component.features.locale.selecting]]
  260. === Selecting a Locale
  261. A common task in many applications is selecting a locale.
  262. The locale can be set for the [classname]#UI# or single [classname]#Component#.
  263. By default each component uses the locale from the [classname]#UI# it has been
  264. attached to. Setting a locale to a [classname]#Component# only applies the locale
  265. to that component and its children. Note, that updating the locale for a component
  266. does not update its children, thus any child component that uses the locale should be updated manually.
  267. [[components.features.readonly]]
  268. == Read-Only
  269. ((("read-only property")))
  270. ((("Component interface", "read-only")))
  271. The property defines whether the value of a component can be changed. The
  272. property is only applicable to components implementing the [interfacename]#HasValue# interface.
  273. [source, java]
  274. ----
  275. TextField readwrite = new TextField("Read-Write");
  276. readwrite.setValue("You can change this");
  277. readwrite.setReadOnly(false); // The default
  278. TextField readonly = new TextField("Read-Only");
  279. readonly.setValue("You can't touch this!");
  280. readonly.setReadOnly(true);
  281. ----
  282. The resulting read-only text field is shown in
  283. <<figure.components.features.readonly.simple>>.
  284. [[figure.components.features.readonly.simple]]
  285. .A read-only component
  286. image::img/features-readonly-simple.png[width=50%, scaledwidth=80%]
  287. Notice that the value of a selection component is the selection, not its items.
  288. A read-only selection component doesn't therefore allow its selection to be
  289. changed, but other changes are possible. For example, if you have a
  290. [classname]#Grid# with a read-only selection model in editable mode,
  291. its contained fields and the underlying data model can still be edited,
  292. and the user could sort it or reorder the columns.
  293. Client-side state modifications will not be communicated to the server-side and,
  294. more importantly, server-side field components will not accept changes to the
  295. value of a read-only [classname]#HasValue# component. The latter is an important
  296. security feature, because a malicious user can not fabricate state changes in a
  297. read-only field.
  298. Also notice that while the read-only status applies automatically to the value
  299. of a field, it does not apply to other component variables. A
  300. read-only component can accept some other state changes from the client-side
  301. and some of such changes could be acceptable, such as change in the scroll bar
  302. position of a [classname]#ListSelect#. Custom components should check the read-only
  303. state for variables bound to business data.
  304. === CSS Style Rules
  305. Setting a normally editable component to read-only state can change its
  306. appearance to disallow editing the value. In addition to CSS styling, also the
  307. HTML structure can change. For example, [classname]#TextField# loses the edit
  308. box and appears much like a [classname]#Label#.
  309. A read-only component will have the [literal]#++v-readonly++# style. The
  310. following CSS rule would make the text in all read-only [classname]#TextField#
  311. components appear in italic.
  312. [source, css]
  313. ----
  314. .v-textfield.v-readonly {
  315. font-style: italic;
  316. }
  317. ----
  318. [[components.features.stylename]]
  319. == Style Name
  320. ((("style name property")))
  321. ((("Component interface", "style name")))
  322. The __style name__ property defines one or more custom CSS style class names for
  323. the component. The [methodname]#getStyleName()# returns the current style names
  324. as a space-separated list. The [methodname]#setStyleName()# replaces all the
  325. styles with the given style name or a space-separated list of style names. You
  326. can also add and remove individual style names with [methodname]#addStylename()#
  327. and [methodname]#removeStyleName()#. A style name must be a valid CSS style
  328. name.
  329. [source, java]
  330. ----
  331. Label label = new Label("This text has a lot of style");
  332. label.addStyleName("mystyle");
  333. layout.addComponent(label);
  334. ----
  335. The style name will appear in the component's HTML element in two forms:
  336. literally as given and prefixed with the component-specific style name. For
  337. example, if you add a style name [literal]#++mystyle++# to a
  338. [classname]#Button#, the component would get both [literal]#++mystyle++# and
  339. [literal]#++v-button-mystyle++# styles. Neither form may conflict with built-in
  340. style names of Vaadin. For example, [literal]#++focus++# style would conflict
  341. with a built-in style of the same name, and an [literal]#++content++# style for
  342. a [classname]#Panel# component would conflict with the built-in
  343. [literal]#++v-panel-content++# style.
  344. The following CSS rule would apply the style to any component that has the
  345. [literal]#++mystyle++# style.
  346. [source, css]
  347. ----
  348. .mystyle {
  349. font-family: fantasy;
  350. font-style: italic;
  351. font-size: 25px;
  352. font-weight: bolder;
  353. line-height: 30px;
  354. }
  355. ----
  356. The resulting styled component is shown in <<figure.components.features.stylename>>
  357. [[figure.components.features.stylename]]
  358. .Component with a custom style
  359. image::img/features-stylename-simple.png[width=50%, scaledwidth=75%]
  360. [[components.features.visible]]
  361. == Visible
  362. ((("visible property")))
  363. ((("Component interface", "visible")))
  364. Components can be hidden by setting the __visible__ property to __false__. Also
  365. the caption, icon and any other component features are made hidden. Hidden
  366. components are not just invisible, but their content is not communicated to the
  367. browser at all. That is, they are not made invisible cosmetically with only CSS
  368. rules. This feature is important for security if you have components that
  369. contain security-critical information that must only be shown in specific
  370. application states.
  371. [source, java]
  372. ----
  373. TextField invisible = new TextField("No-see-um");
  374. invisible.setValue("You can't see this!");
  375. invisible.setVisible(false);
  376. layout.addComponent(invisible);
  377. ----
  378. If you need to make a component only cosmetically invisible, you should use a
  379. custom theme to set it [literal]#++display: none++# style. This is mainly useful
  380. for some special components that have effects even when made invisible in CSS.
  381. If the hidden component has undefined size and is enclosed in a layout that also
  382. has undefined size, the containing layout will collapse when the component
  383. disappears. If you want to have the component keep its size, you have to make it
  384. invisible by setting all its font and other attributes to be transparent. In
  385. such cases, the invisible content of the component can be made visible easily in
  386. the browser.
  387. [[components.features.sizeable]]
  388. == Sizing Components
  389. ((("[classname]#Sizeable# interface")))
  390. Vaadin components are sizeable; not in the sense that they were fairly large or
  391. that the number of the components and their features are sizeable, but in the
  392. sense that you can make them fairly large on the screen if you like, or small or
  393. whatever size.
  394. The [classname]#Sizeable# interface, shared by all components, provides a number
  395. of manipulation methods and constants for setting the height and width of a
  396. component in absolute or relative units, or for leaving the size undefined.
  397. The size of a component can be set with [methodname]#setWidth()# and
  398. [methodname]#setHeight()# methods. The methods take the size as a floating-point
  399. value. You need to give the unit of the measure as the second parameter for the
  400. above methods.
  401. [source, java]
  402. ----
  403. mycomponent.setWidth(100, Unit.PERCENTAGE);
  404. mycomponent.setWidth(400, Unit.PIXELS);
  405. ----
  406. Alternatively, you can specify the size as a string. The format of such a string
  407. must follow the HTML/CSS standards for specifying measures.
  408. [source, java]
  409. ----
  410. mycomponent.setWidth("100%");
  411. mycomponent.setHeight("400px");
  412. ----
  413. The "[literal]#++100%++#" percentage value makes the component take all
  414. available size in the particular direction. You can also use the
  415. shorthand method [methodname]#setSizeFull()# to set the size to 100% in both
  416. directions.
  417. The size can be __undefined__ in either or both dimensions, which means that the
  418. component will take the minimum necessary space. Most components have undefined
  419. size by default, but some layouts have full size in horizontal direction. You
  420. can set the height, width, or both as undefined with the methods [methodname]#setWidthUndefined()#,
  421. [methodname]#setHeightUndefined()#, and [methodname]#setSizeUndefined()#, respectively.
  422. Always keep in mind that _a layout with undefined size may not contain components with defined relative size_, such as "full size", except in some special cases.
  423. See <<../layout/layout-settings#layout.settings.size,"Layout Size">> for details.
  424. If a component inside [classname]#HorizontalLayout# or [classname]#VerticalLayout# has full size in the namesake direction of the layout, the component will expand to take all available space not needed by the other components.
  425. See <<../layout/layout-settings#layout.settings.size,"Layout Size">> for details.
  426. == Managing Input Focus
  427. When the user clicks on a component, the component gets the __input focus__,
  428. which is indicated by highlighting according to style definitions. If the
  429. component allows inputting text, the focus and insertion point are indicated by
  430. a cursor. Pressing the Tab key moves the focus to the component next in the
  431. __focus order__.
  432. Focusing is supported by all [classname]#AbstractField# and [classname]#AbstractListing# components and also by
  433. components such as [classname]#Button#, [classname]#Upload#, and [classname]#TabSheet#.
  434. The focus order or __tab index__ of a component is defined as a positive integer
  435. value, which you can set with [methodname]#setTabIndex()# and get with
  436. [methodname]#getTabIndex()#. The tab index is managed in the context of the page
  437. in which the components are contained. The focus order can therefore jump
  438. between two any lower-level component containers, such as sub-windows or panels.
  439. The default focus order is determined by the natural hierarchical order of
  440. components in the order in which they were added under their parents. The
  441. default tab index is 0 (zero).
  442. Giving a negative integer as the tab index removes the component from the focus
  443. order entirely.
  444. === CSS Style Rules
  445. The component having the focus will have an additional style class with the
  446. [literal]#++-focus++# suffix. For example, a [classname]#TextField#, which
  447. normally has the [literal]#++v-textfield++# style, would additionally have the
  448. [literal]#++v-textfield-focus++# style.
  449. For example, the following would make a text field blue when it has focus.
  450. [source, css]
  451. ----
  452. .v-textfield-focus {
  453. background: lightblue;
  454. }
  455. ----