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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ---
  2. title: Styling a Widget
  3. order: 8
  4. layout: page
  5. ---
  6. [[gwt.styling]]
  7. = Styling a Widget
  8. To make your widget look stylish, you need to style it. There are two basic ways
  9. to define CSS styles for a component: in the widget sources and in a theme. A
  10. default style should be defined in the widget sources, and different themes can
  11. then modify the style.
  12. [[gwt.styling.class]]
  13. == Determining the CSS Class
  14. The CSS class of a widget element is normally defined in the widget class and
  15. set with [methodname]#setStyleName()#. A widget should set the styles for its
  16. sub-elements as it desires.
  17. For example, you could style a composite widget with an overall style and with
  18. separate styles for the sub-widgets as follows:
  19. ----
  20. public class MyPickerWidget extends ComplexPanel {
  21. public static final String CLASSNAME = "mypicker";
  22. private final TextBox textBox = new TextBox();
  23. private final PushButton button = new PushButton("...");
  24. public MyPickerWidget() {
  25. setElement(Document.get().createDivElement());
  26. setStylePrimaryName(CLASSNAME);
  27. textBox.setStylePrimaryName(CLASSNAME + "-field");
  28. button.setStylePrimaryName(CLASSNAME + "-button");
  29. add(textBox, getElement());
  30. add(button, getElement());
  31. button.addClickHandler(new ClickHandler() {
  32. public void onClick(ClickEvent event) {
  33. Window.alert("Calendar picker not yet supported!");
  34. }
  35. });
  36. }
  37. }
  38. ----
  39. In addition, all Vaadin components get the [literal]#++v-widget++# class. If it
  40. extends an existing Vaadin or GWT widget, it will inherit CSS classes from that
  41. as well.
  42. [[gwt.styling.default]]
  43. == Default Stylesheet
  44. A client-side module, which is normally a widget set, can include stylesheets.
  45. They must be placed under the [filename]#public# folder under the folder of the
  46. widget set, a described in
  47. <<dummy/../../../framework/clientside/clientside-module#clientside.module.stylesheet,"Specifying
  48. a Stylesheet">>.
  49. For example, you could style the widget described above as follows:
  50. ----
  51. .mypicker {
  52. white-space: nowrap;
  53. }
  54. .mypicker-button {
  55. display: inline-block;
  56. border: 1px solid black;
  57. padding: 3px;
  58. width: 15px;
  59. text-align: center;
  60. }
  61. ----
  62. Notice that some size settings may require more complex handling and calculating
  63. the sizes dynamically.