Common Component Features

    The interface defines a number of properties, which you can retrieve or manipulate with the corresponding setters and getters.

    A caption is an explanatory textual label accompanying a user interface component, usually shown above, left of, or inside the component. The contents of a caption are automatically quoted, so no raw HTML can be rendered in a caption.

    The caption text can usually be given as the first parameter of a constructor of a component or with setCaption().

    Java

    The caption of a component is, by default, managed and displayed by the layout component or component container inside which the component is placed. For example, the VerticalLayout component shows the captions left-aligned above the contained components, while the FormLayout component shows the captions on the left side of the vertically laid components, with the captions and their associated components left-aligned in their own columns. The CustomComponent does not manage the caption of its composition root, so if the root component has a caption, it will not be rendered.

    Caption Management by VerticalLayout and FormLayout.

    Some components, such as Button and Panel, manage the caption themselves and display it inside the component.

    Icon (see Icon) is closely related to caption and is usually displayed horizontally before or after it, depending on the component and the containing layout. Also the required indicator in field components is usually shown before or after the caption.

    An alternative way to implement a caption is to use another component as the caption, typically a Label, a TextField, or a Panel. A Label, for example, allows highlighting a shortcut key with HTML markup or to bind the caption to a data source. The Panel provides an easy way to add both a caption and a border around a component.

    CSS

    1. .v-captiontext {}
    2. .v-caption-clearelem {}
    3. .v-required-field-indicator {}

    A caption is be rendered inside an HTML element that has the v-caption CSS style class. The containing layout may enclose a caption inside other caption-related elements.

    Some layouts put the caption text in a v-captiontext element. A v-caption-clearelem is used in some layouts to clear a CSS float property in captions. An optional required indicator in field components is contained in a separate element with v-required-field-indicator style.

    Description and Tooltips

    All components (that inherit AbstractComponent) have a description separate from their caption. The description is usually shown as a tooltip that appears when the mouse pointer hovers over the component for a short time.

    You can set the description with setDescription() and retrieve with getDescription().

    Java

    1. Button button = new Button("A Button");
    2. button.setDescription("This is the tooltip");

    The tooltip is shown in Component Description as a Tooltip.

    tooltip plain withpointer hi

    Component Description as a Tooltip

    A description is rendered as a tooltip in most components.

    When a component error has been set with setComponentError(), the error is usually also displayed in the tooltip, below the description. Components that are in error state will also display the error indicator. See .

    The description is actually not plain text, but you can use HTML tags to format it. Such a rich text description can contain any HTML elements, including images.

    Java

    1. button.setDescription(
    2. "<h2><img src=\"../VAADIN/themes/sampler/"+
    3. "icons/comment_yellow.gif\"/>"+
    4. "A richtext tooltip</h2>"+
    5. "<ul>"+
    6. " <li>Use rich formatting with HTML</li>"+
    7. " <li>Include images from themes</li>"+
    8. " <li>etc.</li>"+
    9. "</ul>");

    See the on-line example.

    The result is shown in .

    A Rich Text Tooltip

    Notice that the setter and getter are defined for all fields in the Field interface, not for all components in the Component interface.

    Enabled

    The enabled property controls whether the user can actually use the component. A disabled component is visible, but grayed to indicate the disabled state.

    Components are always enabled by default. You can disable a component with setEnabled(false).

    Java

    1. Button enabled = new Button("Enabled");
    2. enabled.setEnabled(true); // The default
    3. layout.addComponent(enabled);
    4. Button disabled = new Button("Disabled");
    5. disabled.setEnabled(false);
    6. layout.addComponent(disabled);

    shows the enabled and disabled buttons.

    features enabled simple

    An Enabled and Disabled Button

    A disabled component is automatically put in read-only state. No client interaction with such a component is sent to the server and, as an important security feature, the server-side components do not receive state updates from the client in the read-only state. This feature exists in all built-in components in Vaadin and is automatically handled for all Field components for the field property value. For custom widgets, you need to make sure that the read-only state is checked on the server-side for all safety-critical variables.

    CSS Style Rules

    Disabled components have the v-disabled CSS style in addition to the component-specific style. To match a component with both the styles, you have to join the style class names with a dot as done in the example below.

    CSS

    1. .v-textfield.v-disabled {
    2. border: dotted;
    3. }

    This would make the border of all disabled text fields dotted.

    In the Valo theme, the opacity of disabled components is specified with the $v-disabled-opacity parameter, as described in

    An icon is an explanatory graphical label accompanying a user interface component, usually shown above, left of, or inside the component. Icon is closely related to caption (see Caption) and is usually displayed horizontally before or after it, depending on the component and the containing layout.

    The icon of a component can be set with the setIcon() method. The image is provided as a resource, perhaps most typically a ThemeResource.

    Java

    The icon of a component is, by default, managed and displayed by the layout component or component container in which the component is placed. For example, the VerticalLayout component shows the icons left-aligned above the contained components, while the FormLayout component shows the icons on the left side of the vertically laid components, with the icons and their associated components left-aligned in their own columns. The CustomComponent does not manage the icon of its composition root, so if the root component has an icon, it will not be rendered.

    Displaying an Icon from a Theme Resource.

    Some components, such as Button and Panel, manage the icon themselves and display it inside the component.

    An icon will be rendered inside an HTML element that has the v-icon CSS style class. The containing layout may enclose an icon and a caption inside elements related to the caption, such as v-caption.

    Locale

    The locale property defines the country and language used in a component. You can use the locale information in conjunction with an internationalization scheme to acquire localized resources. Some components, such as DateField, use the locale for component localization.

    You can set the locale of a component (or the application) with setLocale() as follows:

    Java

    1. // Component for which the locale is meaningful
    2. InlineDateField date = new InlineDateField("Datum");
    3. // German language specified with ISO 639-1 language
    4. // code and ISO 3166-1 alpha-2 country code.
    5. date.setLocale(new Locale("de", "DE"));
    6. layout.addComponent(date);

    See the on-line example.

    The resulting date field is shown in .

    features locale simple

    Set locale for InlineDateField

    Getting the Locale

    You can get the locale of a component with getLocale(). If the locale is undefined for a component, that is, not explicitly set, the locale of the parent component is used. If none of the parent components have a locale set, the locale of the UI is used, and if that is not set, the default system locale is set, as given by Locale.getDefault().

    The getLocale() returns null if the component is not yet attached to the UI, which is usually the case in most constructors, so it is a bit awkward to use it for internationalization. You can get the locale in attach(), as shown in the following example:

    Java

    1. Button cancel = new Button() {
    2. @Override
    3. public void attach() {
    4. super.attach();
    5. ResourceBundle bundle = ResourceBundle.getBundle(
    6. MyAppCaptions.class.getName(), getLocale());
    7. setCaption(bundle.getString(MyAppCaptions.CancelKey));
    8. }
    9. };

    See the .

    However, it is normally a better practice to use the locale of the current UI to get the localized resource right when the component is created.

    Java

    1. // Captions are stored in MyAppCaptions resource bundle
    2. // and the UI object is known in this context.
    3. ResourceBundle bundle =
    4. ResourceBundle.getBundle(MyAppCaptions.class.getName(),
    5. UI.getCurrent().getLocale());
    6. // Get a localized resource from the bundle
    7. Button cancel =
    8. new Button(bundle.getString(MyAppCaptions.CancelKey));
    9. layout.addComponent(cancel);

    See the on-line example.

    A common task in many applications is selecting a locale. This is done in the following example with a ComboBox, which gets the available locales in Java.

    Java

    1. // The locale in which we want to have the language
    2. // selection list
    3. Locale displayLocale = Locale.ENGLISH;
    4. // All known locales
    5. final Locale[] locales = Locale.getAvailableLocales();
    6. // Allow selecting a language. We are in a constructor of a
    7. // CustomComponent, so preselecting the current
    8. // language of the application can not be done before
    9. // this (and the selection) component are attached to
    10. // the application.
    11. final ComboBox select = new ComboBox("Select a language") {
    12. @Override
    13. public void attach() {
    14. super.attach();
    15. setValue(getLocale());
    16. }
    17. };
    18. for (int i=0; i<locales.length; i++) {
    19. select.addItem(locales[i]);
    20. select.setItemCaption(locales[i],
    21. locales[i].getDisplayName(displayLocale));
    22. // Automatically select the current locale
    23. if (locales[i].equals(getLocale()))
    24. select.setValue(locales[i]);
    25. }
    26. // Locale code of the selected locale
    27. final Label localeCode = new Label("");
    28. layout.addComponent(localeCode);
    29. // A date field which language the selection will change
    30. final InlineDateField date =
    31. new InlineDateField("Calendar in the selected language");
    32. date.setResolution(Resolution.DAY);
    33. layout.addComponent(date);
    34. // Handle language selection
    35. select.addValueChangeListener(new Property.ValueChangeListener() {
    36. public void valueChange(ValueChangeEvent event) {
    37. Locale locale = (Locale) select.getValue();
    38. date.setLocale(locale);
    39. localeCode.setValue("Locale code: " +
    40. locale.getLanguage() + "_" +
    41. locale.getCountry());
    42. }
    43. });
    44. select.setImmediate(true);

    See the .

    The user interface is shown in Selecting a locale.

    Selecting a locale

    Read-Only

    The property defines whether the value of a component can be changed. The property is mainly applicable to Field components, as they have a value that can be edited by the user.

    Java

    1. TextField readwrite = new TextField("Read-Write");
    2. readwrite.setValue("You can change this");
    3. readwrite.setReadOnly(false); // The default
    4. layout.addComponent(readwrite);
    5. TextField readonly = new TextField("Read-Only");
    6. readonly.setValue("You can't touch this!");
    7. readonly.setReadOnly(true);
    8. layout.addComponent(readonly);

    The resulting read-only text field is shown in A read-only component.

    features readonly simple

    A read-only component

    Setting a layout or some other component container as read-only does not usually make the contained components read-only recursively. This is different from, for example, the disabled state, which is usually applied recursively.

    Notice that the value of a selection component is the selection, not its items. A read-only selection component doesn’t therefore allow its selection to be changed, but other changes are possible. For example, if you have a read-only Table in editable mode, its contained fields and the underlying data model can still be edited, and the user could sort it or reorder the columns.

    Client-side state modifications will not be communicated to the server-side and, more importantly, server-side field components will not accept changes to the value of a read-only Field component. The latter is an important security feature, because a malicious user can not fabricate state changes in a read-only field. This is handled at the level of AbstractField in setValue(), so you can not change the value programmatically either. Calling setValue() on a read-only field results in Property.ReadOnlyException.

    Also notice that while the read-only status applies automatically to the property value of a field, it does not apply to other component variables. A read-only component can accept some other variable changes from the client-side and some of such changes could be acceptable, such as change in the scroll bar position of a Table. Custom widgets should check the read-only state for variables bound to business data.

    CSS Style Rules

    Setting a normally editable component to read-only state can change its appearance to disallow editing the value. In addition to CSS styling, also the HTML structure can change. For example, TextField loses the edit box and appears much like a Label.

    A read-only component will have the v-readonly style. The following CSS rule would make the text in all read-only TextField components appear in italic.

    CSS

    The style name property defines one or more custom CSS style class names for the component. The getStyleName() returns the current style names as a space-separated list. The setStyleName() replaces all the styles with the given style name or a space-separated list of style names. You can also add and remove individual style names with addStylename() and removeStyleName(). A style name must be a valid CSS style name.

    Java

    1. Label label = new Label("This text has a lot of style");
    2. label.addStyleName("mystyle");
    3. layout.addComponent(label);

    The style name will appear in the component’s HTML element in two forms: literally as given and prefixed with the component-specific style name. For example, if you add a style name mystyle to a Button, the component would get both mystyle and v-button-mystyle styles. Neither form may conflict with built-in style names of Vaadin. For example, focus style would conflict with a built-in style of the same name, and an content style for a Panel component would conflict with the built-in v-panel-content style.

    The following CSS rule would apply the style to any component that has the mystyle style.

    CSS

    1. .mystyle {
    2. font-family: fantasy;
    3. font-style: italic;
    4. font-size: 25px;
    5. font-weight: bolder;
    6. line-height: 30px;
    7. }

    The resulting styled component is shown in Component with a custom style

    Component with a custom style

    Visible

    Components can be hidden by setting the visible property to false. Also the caption, icon and any other component features are made hidden. Hidden components are not just invisible, but their content is not communicated to the browser at all. That is, they are not made invisible cosmetically with only CSS rules. This feature is important for security if you have components that contain security-critical information that must only be shown in specific application states.

    Java

    1. TextField invisible = new TextField("No-see-um");
    2. invisible.setValue("You can't see this!");
    3. invisible.setVisible(false);
    4. layout.addComponent(invisible);

    The resulting invisible component is shown in An invisible component.

    features visible simple

    An invisible component

    If you need to make a component only cosmetically invisible, you should use a custom theme to set it display: none style. This is mainly useful for some special components that have effects even when made invisible in CSS. If the hidden component has undefined size and is enclosed in a layout that also has undefined size, the containing layout will collapse when the component disappears. If you want to have the component keep its size, you have to make it invisible by setting all its font and other attributes to be transparent. In such cases, the invisible content of the component can be made visible easily in the browser.

    A component made invisible with the visible property has no particular CSS style class to indicate that it is hidden. The element does exist though, but has display: none style, which overrides any CSS styling.

    Sizing Components

    Vaadin components are sizeable; not in the sense that they were fairly large or that the number of the components and their features are sizeable, but in the sense that you can make them fairly large on the screen if you like, or small or whatever size.

    The Sizeable interface, shared by all components, provides a number of manipulation methods and constants for setting the height and width of a component in absolute or relative units, or for leaving the size undefined.

    The size of a component can be set with setWidth() and setHeight() methods. The methods take the size as a floating-point value. You need to give the unit of the measure as the second parameter for the above methods. The available units are listed in Size units below.

    Java

    1. mycomponent.setWidth(100, Sizeable.UNITS_PERCENTAGE);
    2. mycomponent.setWidth(400, Sizeable.UNITS_PIXELS);

    Alternatively, you can speficy the size as a string. The format of such a string must follow the HTML/CSS standards for specifying measures.

    Java

    1. mycomponent.setWidth("100%");

    The “100%” percentage value makes the component take all available size in the particular direction (see the description of Sizeable.UNITS_PERCENTAGE in the table below). You can also use the shorthand method setSizeFull() to set the size to 100% in both directions.

    The size can be undefined in either or both dimensions, which means that the component will take the minimum necessary space. Most components have undefined size by default, but some layouts have full size in horizontal direction. You can set the height or width as undefined with Sizeable.SIZE_UNDEFINED parameter for setWidth() and setHeight().

    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. See for details.

    The Size units table lists the available units and their codes defined in the Sizeable interface.

    If a component inside HorizontalLayout or 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. See for details.

    When the user clicks on a component, the component gets the input focus, which is indicated by highlighting according to style definitions. If the component allows inputting text, the focus and insertion point are indicated by a cursor. Pressing the Tab key moves the focus to the component next in the focus order.

    Focusing is supported by all Field components and also by the Upload component.

    The focus order or tab index of a component is defined as a positive integer value, which you can set with setTabIndex() and get with getTabIndex(). The tab index is managed in the context of the page in which the components are contained. The focus order can therefore jump between two any lower-level component containers, such as sub-windows or panels.

    The default focus order is determined by the natural hierarchical order of components in the order in which they were added under their parents. The default tab index is 0 (zero).

    Giving a negative integer as the tab index removes the component from the focus order entirely.

    The component having the focus will have an additional style class with the -focus suffix. For example, a TextField, which normally has the v-textfield style, would additionally have the v-textfield-focus style.

    CSS