Form.discard() succeed, but doesn't refresh displayed values

I have a trouble with
Form.discard()
method. When I call
discard()
method it restores values in
Item
, but doesn’t refresh displayed fields values. I wrote as simple form as it possible, but failed. I really can’t figure out what have I done wrong and I have no idea after four days of investigations what’s going wrong with this code, so any help and suggestions will be appreciated.


package ru.mx.wordcards.ui;

import com.vaadin.data.util.*;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.*;

public class CardForm extends Form implements Button.ClickListener {

	public CardForm() {

		setWriteThrough(false);

		HorizontalLayout layout = new HorizontalLayout();

		btnSave = new Button("save");
		btnSave.addListener((ClickListener) this);
		layout.addComponent(btnSave);

		btnUndo = new Button("undo");
		btnUndo.addListener((ClickListener) this);
		layout.addComponent(btnUndo);

		btnLink = new Button("link");
		btnLink.addListener((ClickListener) this);
		layout.addComponent(btnLink);

		btnExit = new Button("exit");
		btnExit.addListener((ClickListener) this);
		layout.addComponent(btnExit);

		setFooter(layout);
	}

	public void buttonClick(ClickEvent event) {
		if (event.getButton() == btnSave)
			save();
		else if (event.getButton() == btnUndo)
			undo();
		else if (event.getButton() == btnLink)
			link();
		else if (event.getButton() == btnExit)
			exit();
	}

	public void save() {
		commit();
		getWindow().showNotification("save executed");
	}

	public void undo() {
		/*[b]
this is the place that i can't get
[/b]*/
		discard();
		getWindow().showNotification("undo executed");
	}

	public void exit() {
		getApplication().close();
	}

	public void link() {
		if (getItemDataSource() == null) {
			PropertysetItem item = new PropertysetItem();
			item.addItemProperty("textEn", new ObjectProperty<String>("eng"));
			item.addItemProperty("textRu", new ObjectProperty<String>("rus"));
			setItemDataSource(item);
		} else {
			setItemDataSource(getItemDataSource());
		}
		getWindow().showNotification("link executed");
	}

	private static final long serialVersionUID = 1L;

	private Button btnSave;
	private Button btnUndo;
	private Button btnExit;
	private Button btnLink;
}

Hi,

After discard() call the form.setItemDataSource() method with your Item to refresh the UI.

-Henri

Thanks for your reply, that’s exactly what I use at work :slight_smile: I try to figure out for myself why same code in Vaadin’s Address book tutorial produces normal result:


5.4 Improving the form

public void buttonClick(ClickEvent event) {
         Button source = event.getButton();

        if (source == save) {
             /* If the given input is not valid there is no point in continuing */
             if (!isValid()) {
                 return;
             }
             commit();
             setReadOnly(true);
         } else if (source == cancel) {
             discard();
             setReadOnly(true);
         } else if (source == edit) {
             setReadOnly(false);
         }
     }

Seems like it was because of issue 7788, http://vaadin.com/download/release/6.7/6.7.1/release-notes.html#fixes
In 6.7.2 everything works just fine.

No I don’t think this is fixed. I’m using vaddin 6.8.0, I still see the same issue. Discard doesn’t seem to work the way its supposed to be.

Try 6.8.1 or 6.8.2. I have the recollection there was a discard() related regression in 6.8.0 that was fixed for 6.8.1 - not sure if it was this issue but worth a try.

Found a better way to fix this. You have to deep copy or clone the object and then form.setitemdatasource(deepcopiedobject).

Below is the link which explains how to deep copy java object

http://javatechniques.com/blog/faster-deep-copies-of-java-objects/