Styling widgets

The default CSS style of a custom widget is based on the GWT style. In this recipe, we will show how to change it. As shown in the following screenshot, we will make a simple change. The label of the counter is aligned at the bottom and has a gray color.

Styling widgets

Getting ready

We create a widget text field with a counter as described in the Creating a TextField with counter recipe.

How to do it...

Carry out the following steps to learn how to style a widget:

  1. In the constructor of the CountedTextFieldWidget class, we set the primary CSS style name of each widget because we want to set a new style for a specific widget. The name consists of a base style name plus a general name of the widget separated by a dash.
    public CountedTextFieldWidget() {
      setStylePrimaryName(CLASSNAME);
      textBox.setStylePrimaryName(CLASSNAME + "-field");
      countLabel.setStylePrimaryName(CLASSNAME + "-label");
      …
    }
  2. On the same level as the *.gwt.xml file, we create the public/countedtextfield/styles.css folder with the styles.css file. The folder structure is shown in the following screenshot:
    How to do it...
  3. In the styles.css file, we write the CSS style of our widget.
    .countedtextfield-label{
      color: gray;
      vertical-align: bottom;
    }
  4. Now we can recompile the widget set, run the server, and show the Demo application in the web browser.

How it works...

By default, the class name for each component is gwt-<classname>. For example, the Label widget has a default style of gwt-Label. Using the setStylePrimaryName(String style) method removes the default style and replaces it with our style name. Then we can define this new style in the styles.css file that is placed on the same level as the *.gwt.xml file.

There's more...

We can change each part of our component through CSS. We can also import and use the default CSS styles Reindeer or Runo.

When we want to change the style of all labels, we can do it by the default CSS class name. In that case, we replace the name .countedtextfield-label with .gwt-Label in the styles.css file. And we don't use the setStylePrimaryName() method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.18.33