We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

This tip demonstrates how to add a Wicket component to your web page conditionally. To keep it simple I will add a Wicket Label component to a panel if a List is null otherwise not. Here, in my sample code, I am creating a panel named "ExamplePanel" in which I am loading a list of  "Strings" dynamically.

 

"ExamplePanel.java"

public class ExamplePanel extends Panel {

   //declared a boolean variable and setted it to false

   boolean isListEmpty = false;

  /*
   * constructor here
   */

  public ExamplePanel(String id) {

    super(id);

    /*
     * getting the list of Strings by executing a sql query under getList() method of "ExamplePanelDAO.java".
     * Just suppose that in getList() method a sql query executes and it returns a list of some String object.
    */

    final List<String> list = new ExamplePanelDAO().getList();

    /*
     * Check whether list is empty or not. If it is empty then set "isListEmpty to true"
     */

    if(list.size() == 0)

       isListEmpty = true;

    /*
     * Added a Label here with wicket:id="dataNotFoundLabel". Now the most important thing is that we need to override the "isVisible()" method of Wicket component class.
     * Since we setted "isListEmpty" to true if list is empty, so this label will be visible only if "isListEmpty" will be true. Otherwise Label will not  be visible.
     */

    add(new Label("dataNotFoundLabel","Sorry!! Data Not Available.") {

        public boolean isVisible() {

           return isListEmpty;

        }

     });

  }

}

Another important thing to remember is that when you write markup for the Label in "ExamplePanel.html" you have to write the markup within "<wicket:enclosure>" tag.

"ExamplePanel.html"

<wicket:panel>
<wicket:enclosure>
<label wicket:id="dataNotFoundLabel"></label>
</wicket:enclosure>
</wicket:panel>

 


Related Tags:

Java, Wicket

Author: Pranav Kumar

top

Java

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login