Thanks Igor for your solution.

However, cannot figure out /why/ i can't use a button. 
Let me explain a little about the mechanics of the page (code is at the
bottom). I can use it to edit data of an existing User (pagestate=EDIT) or
create a new one (pagestate=NEW). On entry of the page i check for the
existence of a userid(='code') in de pageparameters and if it is there the
*originalGebruiker* object is filled with its corresponding data. If it
isn't there, the *originalGebruiker* object is filled with blank strings.
The components on the page are binded to the propterties of the *gebruiker*
object. Now when i press the Cancel button, the data that is already entered
by the user must be reset to their original value, regardless of the
pagestate. This can easily be done by copying the *originalGebruiker* object
to the *gebruiker* object and then refresh the page. When i switch to NEW
mode by pressing the newButton, i fill the *originalGebruiker* object with
blank strings and also do a copy from *originalGebruiker* to *gebruiker* and
refresh. This copy however is what fails (see my previous email). The data
the user entered is NOT erased by the *originalGebruiker* data.Nothing
happens.

Here's the code:

public class OnderhoudGebruikerNewPage extends BPRBasePage {

    @SpringBean
    private GebruikerService gebruikerService;
    private enum PageState { EDIT, NEW }
    private AutorisatieGebruiker gebruiker = new AutorisatieGebruiker();
    private AutorisatieGebruiker originalGebruiker = new
AutorisatieGebruiker();
    private String password1;
    private String password2;
    private PageState state = PageState.NEW;

    /**
     * Constructor
     * @param params
     */
    public OnderhoudGebruikerNewPage(PageParameters params) {
        createComponents();
        originalGebruiker = null;
        if (params.containsKey("code")) {
            originalGebruiker =
gebruikerService.find(params.getString("code"));
        }
        if (originalGebruiker == null) {
            originalGebruiker = new AutorisatieGebruiker("", "", "", "");
            state = PageState.NEW;
        } else {
            state = PageState.EDIT;
        }
        resetData();
    }

    private void createComponents() {
        Form form = new Form("form");
        add(form);
        // Add components to the form.
        form.add(new TextField("codeField", new PropertyModel(gebruiker,
"code")).setRequired(true));
        form.add(new TextField("nameField", new PropertyModel(gebruiker,
"naam")).setRequired(true));
        form.add(new TextField("organisationField", new
PropertyModel(gebruiker, "organisatie")));
        form.add(new PasswordTextField("password1Field", new
PropertyModel(this, "password1")));
        form.add(new PasswordTextField("password2Field", new
PropertyModel(this, "password2")));
        form.add(new Button("cancelButton") {
            @Override
            public void onSubmit() {
                processCancel();
            }
        }.setDefaultFormProcessing(false));
        form.add(new Button("saveButton") {
            @Override
            public void onSubmit() {
                processSave();
            }
        });
        form.add(new Button("newButton") {
            @Override
            public void onSubmit() {
                processNew();
            }
        });
        form.add(new Button("deleteButton") {
            @Override
            public void onSubmit() {
                processDelete();
            }
        }.setDefaultFormProcessing(false));
        form.add(new Button("searchButton") {
            @Override
            public void onSubmit() {
                processSearch();
            }
        }.setDefaultFormProcessing(false));
        FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
        add(feedbackPanel);
    }

    /**
     * Fill fields with data.
     */
    private void resetData() {
        // Get references to all fields.
        Form form = (Form) get("form");
        TextField codeField = (TextField) form.get("codeField");
        TextField nameField = (TextField) form.get("nameField");
        TextField organisationField = (TextField)
form.get("organisationField");
        PasswordTextField passwordField1 = (PasswordTextField)
form.get("password1Field");
        PasswordTextField passwordField2 = (PasswordTextField)
form.get("password2Field");

        // Fill them with appropriate data.
        codeField.setDefaultModelObject(originalGebruiker.getCode());
        nameField.setDefaultModelObject(originalGebruiker.getNaam());
       
organisationField.setDefaultModelObject(originalGebruiker.getOrganisatie());
       
passwordField1.setDefaultModelObject(originalGebruiker.getWachtwoord());
        passwordField2.setDefaultModelObject("");
    }

    private void changeState(PageState newState) {
        state = newState;
        // Now change state of some fields based on page state.
        Form form = (Form) get("form");
        TextField codeField = (TextField) form.get("codeField");
        if (state == PageState.EDIT) {
            codeField.setEnabled(false);
        } else {
            codeField.setEnabled(true);
        }
    }

    private void processSearch() {
        // Goto the search page.
        setRedirect(false);
        setResponsePage(OnderhoudGebruikerSearchPage.class);
    }

    private void processDelete() {
        // TODO
    }

    private void processNew() {
        originalGebruiker = new AutorisatieGebruiker("", "", "", "");
        changeState(PageState.NEW);
        resetData();
    }

    private void processSave() {
        // TODO
    }

    private void processCancel() {
        resetData();
    }
}

It would help if you could give me some insight about the reason i cannot
use a button and why the principle i use fails.

Thanks in advance.

--
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/How-can-i-reset-the-data-in-TextFields-tp3625099p3625841.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to