This is what my class basically looks like, hope it helps: -----------------
package my.app; import my.model.AlreadyExistsException; import my.model.DoesNotExistException; import my.model.FailedUpdateException; import my.model.HasChildRelationsException; import my.model.ParentRecord; import my.model.ChildRecord; import org.apache.wicket.behavior.SimpleAttributeModifier; import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.form.Button; 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.model.PropertyModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ChildRecordPanel // extends AbstractTabPanel { private static final long serialVersionUID = 1L; private static Logger log = LoggerFactory.getLogger(ChildRecordPanel.class); private ChildRecord selectedChildRecord; private String unmodifiedChildRecord; @SuppressWarnings({"rawtypes"}) public ChildRecordPanel(String id) { super(id); Form selectForEditForm = new Form("selectForEditForm"); editGroup = new EditGroup("editGroup"); editGroup.setVisible(false); selectForEditForm.add(new DropDownChoice<ChildRecord>("childRecords", new PropertyModel<ChildRecord>(this, "selectedChildRecord"), childRecordsModel, new ChildRecordRenderer()).setNullValid(false)); Button add = new Button("add") { private static final long serialVersionUID = 1L; public void onSubmit() { selectedChildRecord = new ChildRecord(); isNew = true; editLegend = "New"; editGroup.setVisible(true); } }; selectForEditForm.add(add); Button edit = new Button("edit") { private static final long serialVersionUID = 1L; public void onSubmit() { unmodifiedChildRecord = selectedChildRecord.getName(); isNew = false; editLegend = "Edit"; editGroup.setVisible(true); } }; selectForEditForm.add(edit); Button delete = new Button("delete") { private static final long serialVersionUID = 1L; public void onSubmit() { try { unmodifiedChildRecord = selectedChildRecord.getName(); childRecordService.delete(selectedChildRecord.getName(), user); selectedChildRecord = null; info("Deleted '" + unmodifiedChildRecord + "'"); } catch (DoesNotExistException e) { error("'" + selectedChildRecord.getName() + "' does not exist."); } catch (HasChildRelationsException e) { error("Cannot delete until children have been removed."); } // Update the dropdown list // TODO how? // Close the panel editGroup.setVisible(false); } }; // Add confirmation dialog box delete.add(new SimpleAttributeModifier( "onclick", "if (!confirm('Please confirm you want to delete...') ){ return false; }")); selectForEditForm.add(delete); add(selectForEditForm); add(editGroup); } class EditGroup extends WebMarkupContainer { private static final long serialVersionUID = 1L; @SuppressWarnings("rawtypes") public EditGroup(String id) { super(id); add(new Label("editLegend", new PropertyModel(ChildRecordPanel.this, "editLegend"))); Form editForm = new Form("editForm"); editForm.add(new Label("childRecordLabel", "Child")); editForm.add(new Label("parentRecordLabel", "Parent")); editForm.add(new TextField<String>("childRecordText", new PropertyModel<String>(ChildRecordPanel.this, "selectedChildRecord.name"))); editForm.add(new DropDownChoice<ParentRecord>("parents", new PropertyModel<ParentRecord>(ChildRecordPanel.this, "selectedChildRecord.parent"), ChildRecordPanel.this.getParentRecords(), new ParentRecordRenderer()).setNullValid(false)); editForm.add(new Button("save") { private static final long serialVersionUID = 1L; public void onSubmit() { if (isNew) { try { childRecordService.create(selectedChildRecord, user); info("Created '" + selectedChildRecord.getName() + "'"); } catch (AlreadyExistsException e) { error(selectedChildRecord.getName() + " already exists."); } } else { try { childRecordService.update(unmodifiedChildRecord, selectedChildRecord, user); info("Updated '" + selectedChildRecord.getName() + "'"); } catch (FailedUpdateException e) { log.error("Error updating '" + selectedChildRecord.getName() + "'", e); error("An error occured during update. Please contact the system administrator."); } catch (DoesNotExistException e) { error("'" + selectedChildRecord.getName() + "' does not exist."); } } // Update the dropdown list // TODO how? // Close the panel editGroup.setVisible(false); } }); editForm.add(new Button("cancel") { private static final long serialVersionUID = 1L; public void onSubmit() { selectedChildRecord = null; editGroup.setVisible(false); } }.setDefaultFormProcessing(false)); add(editForm); } } // *************************************************************** // These variables/inner classes in the "real" code live in // AbstractTabPanel so that they are accessible by all tab panels // *************************************************************** // TODO set to the logged in user String user = "Unknown"; private boolean isNew = false; private String editLegend; private EditGroup editGroup; class ChildRecordRenderer implements IChoiceRenderer<ChildRecord> { private static final long serialVersionUID = 1L; // Gets the display value that is visible to the end user. public Object getDisplayValue(ChildRecord childRecord) { return childRecord.getName(); } // Gets the value that is invisble to the end user, and that is used as // the selection id. public String getIdValue(ChildRecord childRecord, int index) { return childRecord.getName(); } } class ParentRecordRenderer implements IChoiceRenderer<ParentRecord> { private static final long serialVersionUID = 1L; // Gets the display value that is visible to the end user. public Object getDisplayValue(ParentRecord childRecord) { return parentRecord.getName(); } // Gets the value that is invisble to the end user, and that is used as // the selection id. public String getIdValue(ParentRecord parentRecord, int index) { return parentRecord.getName(); } } LoadableDetachableModel<List<ChildRecord>> childRecordsModel = new LoadableDetachableModel<List<ChildRecord>>() { private static final long serialVersionUID = 1L; @Override protected List<ChildRecord> load() { return childRecordService.getList(); } }; } -----Original Message----- From: jcgarciam [mailto:jcgarc...@gmail.com] Sent: Tuesday, October 05, 2010 1:31 PM To: users@wicket.apache.org Subject: Re: DropDownChoice/Model Confusion There should not need to force the LDB to reload, Can you share more code? On Tue, Oct 5, 2010 at 4:27 PM, Shelli Orton [via Apache Wicket] < ml-node+2956722-1309362030-65...@n4.nabble.com<ml-node%2B2956722-1309362 030-65...@n4.nabble.com> > wrote: > No, I'm not using AJAX. Is there a way to force the LDB to reload? > > Shelli > > -----Original Message----- > From: jcgarciam [mailto:[hidden email]<http://user/SendEmail.jtp?type=node&node=2956722&i=0>] > > Sent: Tuesday, October 05, 2010 12:52 PM > To: [hidden email] <http://user/SendEmail.jtp?type=node&node=2956722&i=1> > Subject: Re: DropDownChoice/Model Confusion > > > I dont think it may be related to the hierarchy, are you using AJAX? > > On Tue, Oct 5, 2010 at 2:38 PM, Shelli Orton [via Apache Wicket] < > [hidden email] <http://user/SendEmail.jtp?type=node&node=2956722&i=2><ml-node%2B2956527 -38526551 > > [hidden email] <http://user/SendEmail.jtp?type=node&node=2956722&i=3>> > > wrote: > > > Hmm, that code sort of works. When I create or edit a record, the > list > > > is updated. However, when I delete a record it is not. My component > > hierarchy is set up like this (hope it makes sense): > > > > TabPanel > > - SelectForEditForm > > - MyRecords (DDC) > > - Add (Button) > > - Edit (Button) > > - Delete (Button) > > - EditGroup (WebMarkupContainer) > > - EditForm > > - MyRecord Name (TextField) > > - MyRecord Attribute (DDC) > > - Save (Button) > > - Cancel (Button) > > > > I created the EditGroup so that I can hide/display the edit panel. > > Debugging the application shows that the save button onSubmit is > called > > before the update of the myRecordsModel LDM and this is why the list > is > > updated when I create/edit a record. The delete button onSubmit is > > called after the update of the myRecordsModel LDM and that is why a > > deleted record is still visible in the list. > > > > I think the issue has something to do with the buttons being in > > different places in the component hierarchy. Can you provide any > > guidance on if there's some way to have the LDM updated after the > delete > > onSubmit? > > > > Thanks again, > > Shelli > > > > -----Original Message----- > > From: jcgarciam [mailto:[hidden > email]<http://user/SendEmail.jtp?type=node&node=2956527&i=0>] > > > > Sent: Tuesday, October 05, 2010 9:33 AM > > To: [hidden email] > <http://user/SendEmail.jtp?type=node&node=2956527&i=1> > > Subject: Re: DropDownChoice/Model Confusion > > > > > > Yes, that should make your DDC to query the list any time it needs to > re > > render. > > > > To your 2nd. question in order to set a default or selected value, > just > > set > > a "MyRecord" reference to your property, keep in mind that your > > "MyRecord" > > should implement and equals/hashcode correctly. > > > > > > On Tue, Oct 5, 2010 at 12:00 PM, Shelli Orton [via Apache Wicket] < > > [hidden email] > <http://user/SendEmail.jtp?type=node&node=2956527&i=2><ml-node%2B2956214 > -1797994 > > > > > [hidden email] <http://user/SendEmail.jtp?type=node&node=2956527&i=3>> > > > wrote: > > > > > Hi, > > > > > > Thanks for replying. Just to clarify, do you mean do something like > > > this: > > > > > > > > > LoadableDetachableModel<List<MyRecord>> myRecordsModel = new > > > LoadableDetachableModel<List<MyRecord>>() > > > { > > > @Override > > > protected List<MyRecord> load() > > > { > > > return myRecordService.getList(); > > > } > > > }; > > > > > > MyRecord selectedRecord; > > > > > > form.add(new DropDownChoice<MyRecord>("records", > > > new PropertyModel<MyRecord>(this, "selectedRecord"), > > > myRecordsModel, > > > new MyRecordRenderer()).setNullValid(false)); > > > > > > Shelli > > > > > > -----Original Message----- > > > From: jcgarciam [mailto:[hidden > > email]<http://user/SendEmail.jtp?type=node&node=2956214&i=0>] > > > > > > Sent: Monday, October 04, 2010 6:27 PM > > > To: [hidden email] > > <http://user/SendEmail.jtp?type=node&node=2956214&i=1> > > > Subject: Re: DropDownChoice/Model Confusion > > > > > > > > > Hi, instead of passing "getMyRecords()" in the constructor wrap it > > > around > > > a LoadableDetachableModel and pass that model to the DDC. The > > difference > > > is > > > that your current list is fetch only once at construction of your > > pages > > > > > and > > > is considered static because its never get called again unless you > > > triggered > > > it by entering to a new page instance, while by using a LDM the > > > component > > > will ask for it every time it needs to re-render them. > > > > > > > > > On Mon, Oct 4, 2010 at 6:52 PM, Shelli Orton [via Apache Wicket] < > > > [hidden email] > > > <http://user/SendEmail.jtp?type=node&node=2956214&i=2><ml-node%2B2955144 > > -66557051 > > > > > > > > [hidden email] > <http://user/SendEmail.jtp?type=node&node=2956214&i=3>> > > > > wrote: > > > > > > > I am writing a (simple?) database CRUD app where I have multiple > tab > > > > panels that encapsulate the different tables in the database. On > a > > > > panel there's a dropdownchoice which lists the existing records > for > > > that > > > > table. There's also create, delete and edit buttons (and and edit > > > panel > > > > with save and cancel buttons). When I create, edit or delete any > of > > > the > > > > records, the dropdownchoice list doesn't reflect the changes until > I > > > > open a new session. So my question is how to reload the list > after > > > > one > > > > of these actions is done? > > > > > > > > Currently my dropdownchoice model is set like this: > > > > > > > > MyRecord selectedRecord; > > > > ... > > > > form.add(new DropDownChoice<MyRecord>("records", > > > > new PropertyModel<MyRecord>(this, "selectedRecord"), > > > > getMyRecords(), // method that retrieves from the database > > > (via > > > > JPA) > > > > new MyRecordRenderer()).setNullValid(false)); // used to > > > return > > > > the display attribute of MyRecord > > > > > > > > I am trying to understand if using a LoadableDetachableModel will > do > > > the > > > > > > > reload that I want. Also, I don't know how to set the > selectedRecord > > > > > using that type of model or if it's even possible. > > > > > > > > Any advice? > > > > > > > > Thanks! > > > > > > > > Shelli > > > > > > > > > > --------------------------------------------------------------------- > > > > > > To unsubscribe, e-mail: [hidden > > > email]<http://user/SendEmail.jtp?type=node&node=2955144&i=0> > > > > For additional commands, e-mail: [hidden > > > email]<http://user/SendEmail.jtp?type=node&node=2955144&i=1> > > > > > > > > > > > > > > > > ------------------------------ > > > > View message @ > > > > > > > > > > http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusio <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi o?by-user=t> > <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi ?by-user=t> > o?by-user=t> > > > <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi ?by-user=t> > <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi ?by-user=t> > ?by-user=t> > > o?by-user=t> > > > n-tp2955144p2955144.html > > > > To start a new topic under Apache Wicket, email > > > > > > > [hidden email] > > > <http://user/SendEmail.jtp?type=node&node=2956214&i=4><ml-node%2B1842946 > > -39801187 > > > > > > [hidden email] > <http://user/SendEmail.jtp?type=node&node=2956214&i=5>> > > > > To unsubscribe from Apache Wicket, click > > > > > > here<http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp< http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?by-us er=t>< > > http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?by-us <http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?by-u s&by-user=t> > er=t>< > > > > > http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?by-us <http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?by-u s&by-user=t> > <http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?by-u <http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?by-u &by-user=t> > s&by-user=t> > > er=t>? > > > > > > > > > tpl=unsubscribe_by_code&node=1842946&code=amNnYXJjaWFtQGdtYWlsLmNvbXwxOD > > > > > > QyOTQ2fDEyNTYxMzc3ODY=>. > > > > > > > > > > > > > > > > > > > > > -- > > > Sincerely, > > > JC (http://www.linkedin.com/in/jcgarciam) > > > Work smarter, not harder!. > > > > > > -- > > > View this message in context: > > > > > > http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusio <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi o?by-user=t> > <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi ?by-user=t> > o?by-user=t> > > > <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi ?by-user=t> > <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi ?by-user=t> > ?by-user=t> > > o?by-user=t> > > > n-tp2955144p2955255.html > > > Sent from the Users forum mailing list archive at Nabble.com. > > > > > > > --------------------------------------------------------------------- > > > To unsubscribe, e-mail: [hidden > > email]<http://user/SendEmail.jtp?type=node&node=2956214&i=6> > > > For additional commands, e-mail: [hidden > > email]<http://user/SendEmail.jtp?type=node&node=2956214&i=7> > > > > > > > > > > --------------------------------------------------------------------- > > > > To unsubscribe, e-mail: [hidden > > email]<http://user/SendEmail.jtp?type=node&node=2956214&i=8> > > > For additional commands, e-mail: [hidden > > email]<http://user/SendEmail.jtp?type=node&node=2956214&i=9> > > > > > > > > > > > > ------------------------------ > > > View message @ > > > > > > http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusio <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi o?by-user=t> > <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi ?by-user=t> > o?by-user=t> > > n-tp2955144p2956214.html > > > To start a new topic under Apache Wicket, email > > > > > [hidden email] > <http://user/SendEmail.jtp?type=node&node=2956527&i=4><ml-node%2B1842946 > -39801187 > > > > [hidden email] <http://user/SendEmail.jtp?type=node&node=2956527&i=5>> > > > To unsubscribe from Apache Wicket, click > > > here<http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp< http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?by-us er=t>< > > http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?by-us <http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?by-u s&by-user=t> > er=t>? > > > > > tpl=unsubscribe_by_code&node=1842946&code=amNnYXJjaWFtQGdtYWlsLmNvbXwxOD > > > QyOTQ2fDEyNTYxMzc3ODY=>. > > > > > > > > > > > > > > > -- > > Sincerely, > > JC (http://www.linkedin.com/in/jcgarciam) > > Work smarter, not harder!. > > > > -- > > View this message in context: > > > http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusio <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi o?by-user=t> > <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi ?by-user=t> > o?by-user=t> > > n-tp2955144p2956283.html > > Sent from the Users forum mailing list archive at Nabble.com. > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [hidden > email]<http://user/SendEmail.jtp?type=node&node=2956527&i=6> > > For additional commands, e-mail: [hidden > email]<http://user/SendEmail.jtp?type=node&node=2956527&i=7> > > > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [hidden > email]<http://user/SendEmail.jtp?type=node&node=2956527&i=8> > > For additional commands, e-mail: [hidden > email]<http://user/SendEmail.jtp?type=node&node=2956527&i=9> > > > > > > > > ------------------------------ > > View message @ > > > http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusio <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi o?by-user=t> > n-tp2955144p2956527.html > > To start a new topic under Apache Wicket, email > > > [hidden email] <http://user/SendEmail.jtp?type=node&node=2956722&i=4><ml-node%2B1842946 -39801187 > > [hidden email] <http://user/SendEmail.jtp?type=node&node=2956722&i=5>> > > To unsubscribe from Apache Wicket, click > here<http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp< http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?by-us er=t>? > > tpl=unsubscribe_by_code&node=1842946&code=amNnYXJjaWFtQGdtYWlsLmNvbXwxOD > QyOTQ2fDEyNTYxMzc3ODY=>. > > > > > > > > > -- > Sincerely, > JC (http://www.linkedin.com/in/jcgarciam) > Work smarter, not harder!. > > -- > View this message in context: > http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusio <http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusi o?by-user=t> > n-tp2955144p2956669.html > Sent from the Users forum mailing list archive at Nabble.com. > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=2956722&i=6> > For additional commands, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=2956722&i=7> > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=2956722&i=8> > For additional commands, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=2956722&i=9> > > > > ------------------------------ > View message @ > http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusio n-tp2955144p2956722.html > To start a new topic under Apache Wicket, email > ml-node+1842946-398011874-65...@n4.nabble.com<ml-node%2B1842946-39801187 4-65...@n4.nabble.com> > To unsubscribe from Apache Wicket, click here<http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp? tpl=unsubscribe_by_code&node=1842946&code=amNnYXJjaWFtQGdtYWlsLmNvbXwxOD QyOTQ2fDEyNTYxMzc3ODY=>. > > > -- Sincerely, JC (http://www.linkedin.com/in/jcgarciam) Work smarter, not harder!. -- View this message in context: http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-Model-Confusio n-tp2955144p2956729.html Sent from the Users forum mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org