How is ObjectProperty updating its datasource?

Hi, I have been testing ObjectProperty with TextField. Here is a sample code:



this.floatValue = new Float(1.0f);
mainLayout.addComponent(new TextField("Some value",
				new ObjectProperty(this.floatValue)));

After this the text field has the value of the ‘floatValue’. My problem is that if I type new value to the text field it is not propagated to the ‘floatValue’. Maybe I have not understood the ObjectProperty usage, am I? :slight_smile:

The value is probagated to the property. (You can not change the value of Float).

Here is working test application to demostrate the behavior:


package fi.jole.test;

import com.itmill.toolkit.Application;
import com.itmill.toolkit.data.util.ObjectProperty;
import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.TextField;
import com.itmill.toolkit.ui.Window;
import com.itmill.toolkit.ui.Button.ClickEvent;

public class TestApp extends Application {

	ObjectProperty property;

	public void init() {

		Window w = new Window("Test Application");
		addWindow(w);
		setTheme("corporate");

		// Create property
		Float floatValue = new Float(1.0f);
		property = new ObjectProperty(floatValue);
		
		// Textfield
		TextField text = new TextField("Some value", property);
		w.addComponent(text);

		// Button to change the property
		w.addComponent(new Button("increase property value", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				Float currentValue = (Float) property.getValue();
				property.setValue(new Float(currentValue.floatValue() + 1.0));
		}}));
		
		// Restart button for appliation 
		// (easier debugging when you dont have to restart the server to make code changes)
		Button restart = new Button("restart", this, "close");
		restart.setStyle("link");
		w.addComponent(restart);

	}
}

To extend this example, here is a simple example on how to use buffering with TextField (or any other Field) and data sources:


package fi.jole.test;

import com.itmill.toolkit.Application;
import com.itmill.toolkit.data.util.MethodProperty;
import com.itmill.toolkit.data.util.ObjectProperty;
import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.TextField;
import com.itmill.toolkit.ui.Window;
import com.itmill.toolkit.ui.Button.ClickEvent;

public class TestApp extends Application {

	ObjectProperty property;
	TextField text;
	
	public void init() {

		Window w = new Window("Test Application");
		addWindow(w);
		setTheme("corporate");

		// Create property
		Float floatValue = new Float(1.0f);
		property = new ObjectProperty(floatValue);
		
		// Textfield
		text = new TextField("Some value", property);
		text.setImmediate(true);
		text.setWriteThrough(false);
		w.addComponent(text);

		// Property state
		Label propertyState = new Label(property);
		propertyState.setCaption("Property (data source) state");
		w.addComponent(propertyState);

		// Button state
		Label textState = new Label(text);
		textState.setCaption("TextField state");
		w.addComponent(textState);

		// Button to change the property
		w.addComponent(new Button("increase property value", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				Float currentValue = (Float) property.getValue();
				property.setValue(new Float(currentValue.floatValue() + 1.0));
		}}));
		
		// Buffering
		w.addComponent(new Button("Write through enabled",new MethodProperty(text, "writeThrough")));
		w.addComponent(new Button("discard", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				text.discard();
			}}));
		w.addComponent(new Button("commit", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				text.commit();
		}}));

		// Restart button for appliation 
		// (easier debugging when you dont have to restart the server to make code changes)
		Button restart = new Button("restart", this, "close");
		restart.setStyle("link");
		w.addComponent(restart);

	}
}