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.

components-customfield.asciidoc 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ---
  2. title: Composite Fields with CustomField
  3. order: 32
  4. layout: page
  5. ---
  6. [[components.customfield]]
  7. = Composite Fields with [classname]#CustomField#
  8. The [classname]#CustomField# is a way to create composite components as with [classname]#CustomComponent#, except that it implements the [interfacename]#Field# interface and inherits [classname]#AbstractField#, described in <<dummy/../../../framework/components/components-fields#components.fields,"Field Components">>.
  9. A field allows editing a property value in the Vaadin data model, and can be bound to data with field groups, as described in <<dummy/../../../framework/datamodel/datamodel-itembinding#datamodel.itembinding, "Creating Forms by Binding Fields to Items">>.
  10. The field values are buffered and can be validated with validators.
  11. A composite field class must implement the [methodname]#getType()# and [methodname]#initContent()# methods.
  12. The latter should return the content composite of the field.
  13. It is typically a layout component, but can be any component.
  14. It is also possible to override [methodname]#validate()#,
  15. [methodname]#setInternalValue()#, [methodname]#commit()#,
  16. [methodname]#setPropertyDataSource#, [methodname]#isEmpty()# and other methods
  17. to implement different functionalities in the field. Methods overriding
  18. [methodname]#setInternalValue()# should call the superclass method.
  19. [[components.customfield.basic]]
  20. == Basic Use
  21. Let us consider a simple custom switch button component that allows you to click a button to switch it "on" and "off", as illustrated in <<figure.components.customfield.basic>>.
  22. [[figure.components.customfield.basic]]
  23. .A custom switch button field
  24. image::img/customfield-basic.png[width=25%, scaledwidth=40%]
  25. The field has [classname]#Boolean# value type, which the [methodname]#getType()# returns.
  26. In [methodname]#initContent()#, we initialize the button and the layout.
  27. Notice how we handle user interaction with the button to change the field value.
  28. We customize the [methodname]#setValue()# method to reflect the state back to the user.
  29. [source, Java]
  30. ----
  31. public class BooleanField extends CustomField<Boolean> {
  32. Button button = new Button();
  33. public BooleanField() {
  34. setValue(true); // On by default
  35. }
  36. @Override
  37. protected Component initContent() {
  38. // Flip the field value on click
  39. button.addClickListener(click ->
  40. setValue(! (Boolean) getValue()));
  41. return new VerticalLayout(
  42. new Label("Click the button"), button);
  43. }
  44. @Override
  45. public Class<Boolean> getType() {
  46. return Boolean.class;
  47. }
  48. @Override
  49. public void setValue(Boolean newFieldValue)
  50. throws com.vaadin.data.Property.ReadOnlyException,
  51. ConversionException {
  52. button.setCaption(newFieldValue? "On" : "Off");
  53. super.setValue(newFieldValue);
  54. }
  55. }
  56. ----
  57. We can now use the field in all the normal ways for a field:
  58. [source, Java]
  59. ----
  60. // Create it
  61. BooleanField field = new BooleanField();
  62. // It's a field so we can set its value
  63. field.setValue(new Boolean(true));
  64. // ...and read the value
  65. Label value = new Label(field.getValue()?
  66. "Initially on" : "Initially off");
  67. // ...and handle value changes
  68. field.addValueChangeListener(event ->
  69. value.setValue(field.getValue()?
  70. "It's now on" : "It's now off"));
  71. ----