Through the Property
interface,
we can easily bind a single data object with a component. This property can be shared between multiple components. Changes in the property in one component are immediately visible in the other components. The Property
interface is the base of the Vaadin Data Model. It provides a standardized API for a single data object that can be read (get) and written (set). In this recipe, we will work with one ObjectProperty
that will store a string of an HTML page. We will create a simple editor of this page. It will consist of three tabs: a preview of a page, an HTML editor, and a Rich text editor as depicted in the following screenshot:
Carry out the following steps to create a simple HTML editor by binding property to a component:
Demo
as follows:public class Demo extends UI {…}
Editor
that extends TabSheet
as follows. Each section (preview and two editors) will have its own tab.public class Editor extends TabSheet {…}
ObjectProperty
class. We call it htmlPage
and we set the type of the value to String
. Next, we add constant of tab page height.private ObjectProperty<String> htmlPage = new ObjectProperty<String>( "<h1>Vaadin</h1><p>is a <b>Java framework</b> for building modern web applications.</p>"); private static final int HEIGHT = 300;
public Editor() { addTab(createPreview()); addTab(createHtmlEditor()); addTab(createRichEditor()); }
createPreview()
method. In all tabs, vertical layout is used. To clarify the code, we move the creation of this layout to the createLayout()
method. The Preview tab consists of only one component. It is a Label
that has set the shared property named htmlPage
.private Layout createPreview() { Layout layout = createLayout("Preview"); Label label = new Label("", ContentMode.HTML); label.setPropertyDataSource(htmlPage); layout.addComponent(label); return layout; }
htmlPage
.private Layout createHtmlEditor() { Layout layout = createLayout("HTML editor"); TextArea editor = new TextArea(); editor.setSizeFull(); editor.setPropertyDataSource(htmlPage); layout.addComponent(editor); return layout; }
RichTextArea
. It has also a set property htmlPage
.private Layout createRichEditor() { Layout layout = createLayout("Rich text editor"); RichTextArea editor = new RichTextArea(); editor.setSizeFull(); editor.setPropertyDataSource(htmlPage); layout.addComponent(editor); return layout; }
private Layout createLayout(String caption) { Layout layout = new VerticalLayout(); layout.setCaption(caption); layout.setHeight(HEIGHT, Unit.PIXELS); return layout; }
Editor
class in the main UI class Demo
.public class Demo extends UI { @Override protected void init(VaadinRequest request) { setContent(new Editor()); } }
We run the server and open the created application in the web browser.
We created a simple HTML editor. The HTML source code is stored in an instance of the ObjectProperty<String>
class. The ObjectProperty
class is a simple data object containing one typed value. In our case, it is a String
type. This property is bound with all three sections of our editor by the
setPropertyDataSource(Property)
method. If the user makes any changes in one of the editors, these changes are propagated into the other sections through ValueChangeEvent
.
In Vaadin, we can use other
implementations of the Property
interface. Some of them are described in the following table:
Properties
is described in Vaadin's book, available at https://vaadin.com/book/vaadin7/-/page/datamodel.properties.htmlProperty
interface is available at https://vaadin.com/api/7.0.0/com/vaadin/data/Property.htmlObjectProperty
class is available at https://vaadin.com/api/7.0.0/com/vaadin/data/util/ObjectProperty.html18.223.108.105