*This is my form source code* package com.jasp.ecommfwk.pages.forms;
import java.io.Serializable; import java.util.List; import org.apache.log4j.Logger; import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior; import org.apache.wicket.ajax.markup.html.form.AjaxButton; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.markup.html.panel.FeedbackPanel; import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.model.Model; import org.apache.wicket.model.StringResourceModel; import org.hibernate.Transaction; import org.hibernate.classic.Session; import com.jasp.ecommfwk.util.markup.html.form.CategoriaChoiceRenderer; import com.jasp.persistence.hierarchy.Categoria; import com.jasp.persistence.hierarchy.dao.CategoriaDAO; import com.jasp.persistence.util.HibernateUtil; @SuppressWarnings("unchecked") public class CategoriaForm extends Form<Categoria> implements Serializable { private static final long serialVersionUID = -1940617589157076677L; private static Logger logger = Logger.getLogger(CategoriaForm.class); private Session hibernateSession; private transient Transaction hibernateTransaction; // Textos i18n carregados aqui private final StringResourceModel labelCategoriaValue = new StringResourceModel( "categoria.label", this, null, new Object[] { getLocale() }); private final StringResourceModel labelPaiValue = new StringResourceModel( "categoria.labelPai", this, null, new Object[] { getLocale() }); // End of i18n load // Declare component's form private DropDownChoice categoriaPai; private TextField nomeCategoria; private Label labelCategoria; private Label labelCategoriaPai; private CategoriaChoiceRenderer choiceRenderer; private AjaxButton cadastrar; private FeedbackPanel feedbackPanel; private void inicializaComponentesForm() { categoriaPai = criaCategoriaPaiDropdown(); nomeCategoria = criaNomeCategoriaTextField(); nomeCategoria.setRequired(true); labelCategoria = new Label("labelCategoria", labelCategoriaValue); labelCategoriaPai = new Label("labelCategoriaPai", labelPaiValue); cadastrar = new AjaxButton("cadastrar", this) { private static final long serialVersionUID = 6045168066074475539L; @Override protected void onSubmit(AjaxRequestTarget target, Form<?> form) { getHibernateTransaction(); int idBanco = Integer.parseInt(categoriaPai.getValue()); Categoria escolhida = null; Categoria nova = new Categoria(); nova.setNome(nomeCategoria.getValue()); hibernateSession.persist(nova); if (0 < idBanco) { escolhida = new Categoria(idBanco); hibernateSession.clear(); //Seto o pai nova.setCategoriaPai(escolhida); hibernateSession.merge(nova); } hibernateTransaction.commit(); closeTransaction(); String mensageSucesso = "Categoria " + nova.getNome() + " cadastrada com sucesso!"; info(mensageSucesso); target.addComponent(feedbackPanel); /* This is the part that I am having trouble with. The idea is after the form submission the Dropdown categoriaPai should be updated with the recent created item */ this.add(new AjaxFormSubmitBehavior("onchange"){ @Override protected void onError(AjaxRequestTarget target) { // TODO Auto-generated method stub } @Override protected void onSubmit(AjaxRequestTarget target) { // TODO Auto-generated method stub target.addComponent(categoriaPai); } }); } @Override protected void onError(AjaxRequestTarget target, Form<?> form) { // repaint the feedback panel so errors are shown target.addComponent(feedbackPanel); } }; } public CategoriaForm(String id) { super(id); setOutputMarkupId(true); inicializaComponentesForm(); feedbackPanel = criaFeedbackPanel(); // Adicionando todos os componentes ao form this.add(categoriaPai); this.add(nomeCategoria); this.add(labelCategoria); this.add(labelCategoriaPai); this.add(cadastrar); this.add(feedbackPanel); } /* This is the method used to create the DropDown */ private DropDownChoice criaCategoriaPaiDropdown() { setOutputMarkupId(true); choiceRenderer = new CategoriaChoiceRenderer(); getHibernateTransaction(); List<Categoria> data = (List<Categoria>) CategoriaDAO .getAllCategorias(hibernateSession); CompoundPropertyModel categoriaPaiModel = new CompoundPropertyModel(data); categoriaPai = new DropDownChoice("categoriaPai",categoriaPaiModel, data, choiceRenderer); return categoriaPai; } private TextField criaNomeCategoriaTextField() { return new TextField("nomeCategoria", new Model("")); } public void setSession(Session session) { this.hibernateSession = session; } public DropDownChoice getCategoriaPai() { return categoriaPai; } public void setCategoriaPai(DropDownChoice categoriaPai) { this.categoriaPai = categoriaPai; } public TextField getNomeCategoria() { return nomeCategoria; } public void setNomeCategoria(TextField nomeCategoria) { this.nomeCategoria = nomeCategoria; } public StringResourceModel getLabelCategoriaValue() { return labelCategoriaValue; } public StringResourceModel getLabelPaiValue() { return labelPaiValue; } public Label getLabelCategoria() { return labelCategoria; } public Label getLabelCategoriaPai() { return labelCategoriaPai; } private Session getHibernateSession() { if (null == hibernateSession || !hibernateSession.isConnected()) { hibernateSession = HibernateUtil.getSessionFactory().openSession(); } return hibernateSession; } public void setHibernateSession(Session hibernateSession) { this.hibernateSession = hibernateSession; } private Transaction getHibernateTransaction() { getHibernateSession(); if (null == hibernateTransaction || !hibernateTransaction.isActive()) { hibernateTransaction = hibernateSession.beginTransaction(); } return hibernateTransaction; } public void setHibernateTransaction(Transaction hibernateTransaction) { this.hibernateTransaction = hibernateTransaction; } private FeedbackPanel criaFeedbackPanel() { FeedbackPanel feedbackPanel = new FeedbackPanel("feedback"); feedbackPanel.setOutputMarkupId(true); return feedbackPanel; } private void closeTransaction(){ closeSession(); hibernateTransaction = null; } private void closeSession() { if (null != hibernateSession && (hibernateSession.isConnected() || hibernateSession.isOpen())) { hibernateSession.close(); } } } *And this is the render used* package com.jasp.ecommfwk.util.markup.html.form; import java.util.List; import org.apache.wicket.markup.html.form.ChoiceRenderer; import com.jasp.persistence.hierarchy.Categoria; public class CategoriaChoiceRenderer extends ChoiceRenderer{ /** * */ private static final long serialVersionUID = -3455900118160934254L; @Override public Object getDisplayValue(Object object) { if (object instanceof Categoria) { Categoria cat = (Categoria) object; return cat.getNome(); } else { throw new IllegalArgumentException( "O objeto deve ser uma instancia de Categoria"); } } @Override public String getIdValue(Object object, int index) { String retorno = null; Categoria cat = null; if (object instanceof Categoria) { cat = (Categoria) object; } else { if(object instanceof List){ List <Categoria> temp = (List<Categoria>) object; if(temp.size()>0){ cat = temp.get(0); }else{ cat = new Categoria(); } }else{ throw new IllegalArgumentException( "O objeto deve ser uma instancia de Categoria"); } } retorno = String.valueOf(cat.getIdCategoria()); return retorno; } } -- "Two rules to succeed in life: 1 - donĀ“t tell people everything you know." -------- We shall go on to the end. We shall fight in France We shall fightover the seas and oceans. We shall fight with growing confidence and growing strength in the air. We shall defend our island whatever the cost may be We shall fight on beaches, we shall fight on the landing grounds, We shall fight in the fields and in the streets, We shall fight on the hills. We shall never surrender. Winston Churchill