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 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ---
  2. title: Composite Fields with CustomField
  3. order: 32
  4. layout: page
  5. ---
  6. [[components.customfield]]
  7. = Composite Fields with 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 data model, and can be bound to data with [classname]#Binder#, as described in <<dummy/../../../framework/datamodel/datamodel-forms#datamodel.forms, "Binding Data to Forms">>.
  10. A composite field class must implement [methodname]#initContent()# method.
  11. It should return the content composite of the field.
  12. Methods overriding
  13. [methodname]#setInternalValue()# should call the superclass method.
  14. [[components.customfield.basic]]
  15. == Basic Use
  16. 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>>.
  17. [[figure.components.customfield.basic]]
  18. .A custom switch button field
  19. image::img/customfield-basic.png[width=25%, scaledwidth=40%]
  20. The field has [classname]#Boolean# value type, which the [methodname]#getType()# returns.
  21. In [methodname]#initContent()#, we initialize the button and the layout.
  22. Notice how we handle user interaction with the button to change the field value.
  23. We customize the [methodname]#setValue()# method to reflect the state back to the user.
  24. [source, Java]
  25. ----
  26. public class BooleanField extends CustomField<Boolean> {
  27. private final Button button = new Button("Off");
  28. private boolean value;
  29. @Override
  30. protected Component initContent() {
  31. button.addClickListener(event -> {
  32. setValue(!getValue());
  33. button.setCaption(getValue() ? "On" : "Off");
  34. });
  35. VerticalLayout layout = new VerticalLayout();
  36. layout.addComponent(new Label("Click the button"));
  37. layout.addComponent(button);
  38. return layout;
  39. }
  40. @Override
  41. public Boolean getValue() {
  42. return value;
  43. }
  44. @Override
  45. protected void doSetValue(Boolean value) {
  46. this.value = value;
  47. button.setCaption(value ? "On" : "Off");
  48. }
  49. }
  50. ----
  51. We can now use the field in all the normal ways for a field:
  52. [source, Java]
  53. ----
  54. // Create it
  55. BooleanField field = new BooleanField();
  56. // It's a field so we can set its value
  57. field.setValue(new Boolean(true));
  58. // ...and read the value
  59. Label value = new Label(field.getValue()?
  60. "Initially on" : "Initially off");
  61. // ...and handle value changes
  62. field.addValueChangeListener(event ->
  63. value.setValue(field.getValue()?
  64. "It's now on" : "It's now off"));
  65. ----