List View is just a way to show your data in a table like structure with as many as rows and columns. In Wicket framework its very easy to use.
Now we see how we use it in Wicket Framework:-
For using ListView first you will have to create a bean(POJO) file with all fields needed in your list view. After that create “ListView” component and add it under any wicket container (exm- Form,Panel etc…).
Here In the follwing code example I am adding “ListView” component to Panel named “ListViewPanel”.
First I am creating a bean file named “ListViewBean.java” which contains only two fields “id” and “name” to keep it simple.
code for ListViewBean.java“:-
public class ListBean { private int id; private String name; /* * constructor here */ public ARBean(int id,String name) { this.id = id; this.name = name; } /* * getter/setter methods */ public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } Now I am creating a Panel named "ListViewPanel" which contains ListView component. code for ListViewPanel.java":- class ListViewPanel extends Panel { public ListViewPanel (String id) { // created a list of ListViewBean object List list = new ArrayList(); /* * Here we are populating the list with some "ListViewBean" objects.. * You can populate the list dynamically with list of "ListViewBean" objects according to your application requirement. */ list.add(new ListViewBean(1,"abc")); list.add(new ListViewBean(2,"xyz")); list.add(new ListViewBean(3,"def")); list.add(new ListViewBean(4,"jkl")); /* created list view here with wicket:id="listView" by which this will be *rendered in markup.As second parameter we pass the above list of "ListViewBean" *object which contains data for list view. */ ListView listView = new ListView("listView", list) { /* * overriding the populateItem() method of "ListView" class here. */ protected void populateItem(ListItem item) { /* getting the "ListViewBean" object by calling getModelObject() of * "ListItem" class .Just like an iterator it iterates over all * object of "ListViewBean" under list. */ ListViewBean listViewBean = (ListViewBean )item.getModelObject(); /* * added labels for all required columns of list view */ item.add(new Label("id", listViewBean .getId())); item.add(new Label("name", listViewBean .getName())); } }; //added list view to panel here add(listView); } }
code for ListViewPanel.java“:-
class ListViewPanel extends Panel { public ListViewPanel (String id) { // created a list of ListViewBean object List list = new ArrayList(); /* * Here we are populating the list with some "ListViewBean" objects.. * You can populate the list dynamically with list of "ListViewBean" objects according to your application requirement. */ list.add(new ListViewBean(1,"abc")); list.add(new ListViewBean(2,"xyz")); list.add(new ListViewBean(3,"def")); list.add(new ListViewBean(4,"jkl")); /* created list view here with wicket:id="listView" by which this will be *rendered in markup.As second parameter we pass the above list of "ListViewBean" *object which contains data for list view. */ ListView listView = new ListView("listView", list) { /* * overriding the populateItem() method of "ListView" class here. */ protected void populateItem(ListItem item) { /* getting the "ListViewBean" object by calling getModelObject() of * "ListItem" class .Just like an iterator it iterates over all * object of "ListViewBean" under list. */ ListViewBean listViewBean = (ListViewBean )item.getModelObject(); /* * added labels for all required columns of list view */ item.add(new Label("id", listViewBean .getId())); item.add(new Label("name", listViewBean .getName())); } }; //added list view to panel here add(listView); } }
Now create markup file for “ListViewPanel”.
Code for ListViewPanel.html”:-
ID | Name |
---|---|
By this way you can create a “ListView” with as many as columns required. In the same way you can also use “PageableListView” instead of “ListView”.The benefit of “PageableListView” is that it also provides you a default pagination if number of rows in list view is more than passed number of rows in the constructor of “PageableListView”. Constructor of “PageableListView” contains one more parameter “int row” compare to simple “ListView” discussed above.
Hope above tip will be helpful for those who is working with wicket or will get a chace to play around Wicket in future.