I have a Table that is populated with a BeanItemContainer. In addition to columns for the members of the POJOs, I’d like to generate an additional sortable column that contains text. The problem is that the generated column is not sortable, even though it returns a Label field. Here’s a simplified example:
myTable.addGeneratedColumn(“extraInfo”, new ColumnGenerator() {
public Component generateCell(Table source, Object itemId,
Object columnId) {
Label label = new Label(“extra info”);
return label;
}
});
Is there something I’m missing to make this sortable?
As an alternative, I also tried this, but it throws an UnsupportedOperationException:
this.addContainerProperty(“extraProp”, String.class, “Some extra info”);
Sorting is done in the container, not in the table; when you add a GeneratedColumn to a table, it is added to the table, not to the container (if you see what I mean), and therefore it is not sortable.
What I have done in a similar scenario is to create and use a “bean wrapper” that returns the extra info. e.g.
class Person {
String name;
Date birthday;
public String getName(){return name};
public Date getBirthday(){return birthday};
}
class PersonWrapper {
Person delegate;
pubic PersonWrapper(Person delegate){
this.delegate = delegate;
}
public String getName(){return delegate.getName();};
public Date getBirthday(){return birthday.getBirthday()};
public int getAge(){
Calendar dateOfBirthCal = Calendar.getInstance();
dateOfBirthCal.set(birthday);
Calendar todayCal = Calendar.getInstance();
int age = todayCal.get(Calendar.YEAR) - dateOfBirthCal.get(Calendar.YEAR);
if (todayCal.get(Calendar.DAY_OF_YEAR) < dateOfBirthCal.get(Calendar.DAY_OF_YEAR)){
age--;
}
return age;
}
}
So, when adding to the beanContainer, instead of doing
BeanItemContainer<Person> container = new BeainItemContainer(Person.class);
for(Person person : people){
container.addBean(person);
}
, you can do this
BeanItemContainer<PersonWrapper> container = new BeainItemContainer(PersonWrapper.class);
for(Person person : people){
container.addBean(new PersonWrapper(person));
}
Just wondering, if you have a POJO with members that are other POJOs, do you generally create extra getter methods for these for use as a BeanItemContainer? Looking for some hints on best practices.
For instance, if a Person Object contains an Address object, would you add extra getters in your PersonWrapper?: