Styling a Widget

To make your widget look stylish, you need to style it. There are two basic ways to define CSS styles for a component: in the widget sources and in a theme. A default style should be defined in the widget sources, and different themes can then modify the style.

Determining the CSS Class

The CSS class of a widget element is normally defined in the widget class and set with setStyleName(). A widget should set the styles for its sub-elements as it desires.

For example, you could style a composite widget with an overall style and with separate styles for the sub-widgets as follows:

Java

  1. public class MyPickerWidget extends ComplexPanel {
  2. public static final String CLASSNAME = "mypicker";
  3. private final TextBox textBox = new TextBox();
  4. private final PushButton button = new PushButton("...");
  5. public MyPickerWidget() {
  6. setElement(Document.get().createDivElement());
  7. setStylePrimaryName(CLASSNAME);
  8. textBox.setStylePrimaryName(CLASSNAME + "-field");
  9. button.setStylePrimaryName(CLASSNAME + "-button");
  10. add(textBox, getElement());
  11. add(button, getElement());
  12. button.addClickHandler(new ClickHandler() {
  13. public void onClick(ClickEvent event) {
  14. Window.alert("Calendar picker not yet supported!");
  15. }
  16. });
  17. }
  18. }

In addition, all Vaadin components get the v-widget class. If it extends an existing Vaadin or GWT widget, it will inherit CSS classes from that as well.

Default Stylesheet

A client-side module, which is normally a widget set, can include stylesheets. They must be placed under the public folder under the folder of the widget set, a described in “Specifying a Stylesheet”.

For example, you could style the widget described above as follows:

  1. .mypicker {
  2. white-space: nowrap;
  3. }
  4. .mypicker-button {
  5. display: inline-block;
  6. border: 1px solid black;
  7. padding: 3px;
  8. width: 15px;
  9. text-align: center;
  10. }

Notice that some size settings may require more complex handling and calculating the sizes dynamically.