Search code examples
formswicketpanelreusabilitysubclass

Wicket - Reusable panels with java-subclasses


  1. I have a java class:
    public Task {

        private int id;
        private Company sender;
        private Company receiver;

        //Getter and Setter
        ...
    }

As you can see, I have 2 other custom classes in the task class. And a company has for example Adress and Directory (see screenshot below).

  1. Now I have a form page with 2 sections (sender & receiver) representing a company. I don't want to make 2 seperate markups and java code for those 2 sections. Any way to avoid this duplication?

enter image description here


Solution

  • You could create a CompanyPanel which takes a IModel<Company>. You could use a PropertyModel on your task class to get one. PropertyModel sender = new PropertyModel(myTask, "sender"). The panel then can have two TextFields for which you can use a CompoundPropertyModel on the IModel that is passed.

    Reuse this panel twice on your form.

    On the CompanyPanel

    public class CompanyPanel extends Panel
    {
        public CompanyPanel(String id, IModel<Company> model)
        {
            super(id, new CompoundPropertyModel(model));
            add( new TextField("address"));
            add( new TextField("directory"));
        }
    }
    

    Lookup the CompoundPropertyModel in the docs. It is really useful.