best way to display indexed elements in array from BeanContainer in Table

Hi,

I have an array inside a bean and that bean is contained in a BeanContainer which is the container for a Table. How do I cause each element in the array to be displayed, without having to write an explicit getter for each element in the array.
As you can see below in my Application class, I try:


		table.setVisibleColumns(new String[] {"dailyTimeArray[0]
", "total"});

but this fails because "Ids must exist in the Container or as a generated column , missing id: dailyTimeArray[0]
"
Any ideas?

A snip from my Application class:


Table table = new Table();
		
		final BeanContainer<String, TimeSummaryRow> beanContainer = new BeanContainer<String, TimeSummaryRow>(TimeSummaryRow.class);
		beanContainer.setBeanIdProperty("title");

		
		
		TimeSummaryRow row = new TimeSummaryRow();
		row.setTitle("title1");
		Double[] dailyTimeArray = new Double[]
 {7d,7d,7d,7d,7d,7d,7d,7d,7d,7d,7d,7d,7d,7d};
		row.setDailyTimeArray(dailyTimeArray);
		
		BeanItem<TimeSummaryRow> beanItem = beanContainer.addBean(row);
		

		table.setContainerDataSource(beanContainer);
		
	
		table.setVisibleColumns(new String[] {"dailyTimeArray[0]
", "total"});

and the referenced bean:


public class TimeSummaryRow {
	public TimeSummaryRow() {
		super();
	}
	private String title;
	private Double[] dailyTimeArray;
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Double[] getDailyTimeArray() {
		return dailyTimeArray;
	}
	public Double getDailyTimeArray(int index) {
		return dailyTimeArray[index]
;
	}
	public void setDailyTimeArray(int index, Double value) {

	}
	public void setDailyTimeArray(Double[] dailyTimeArray) {
		this.dailyTimeArray = dailyTimeArray;
	}
	public double getTotal() {
		return 5;
	}

	
	
}

Hi Kevin,

After read your post I see this fragment

This tells you that the id is not present in your bean, and it’s right, your bean is:


public class TimeSummaryRow {
	public TimeSummaryRow() {
		super();
	}
	private String title;
	private Double[] dailyTimeArray;

        getters and setters

The id that the container is looking for, in fact doesn’t exist in your bean, but dailyTimeArray yes, so, you should change your code from this:


    table.setVisibleColumns(new String[] {"dailyTimeArray[0]
", "total"});

to this:


    table.setVisibleColumns(new String[] {"dailyTimeArray", "total"});

And your getter should do the rest.

HTH.

Javi

Thanks, Javi.

When I try that, it displays this in the cell of the table:

[Ljava.lang.Double;@316c171f

I don’t want this; rather, I want each of the values inside the array, and I may want particular elements of the array, say elements 1, 3, and 4, but not 0, 2, and 5.
That’s why I thought I needed to pass the index value as part of the visible columns.

Any other ideas?

Hi Kevin,

I think that you need to implement a custom Formatting on the columns of your table, this is explained in deep in the chapter
5.14 Tables
in the book of Vaadin, take a look and let me know.

Cheers.

Javi

thanks, I’ve read that chapter a few times and I think the closest thing that I’m looking for is a ColumnGenerator.
In the end, I decided to just add explicit getters in my bean for each possible element in my array…I think it works out to about the same amount of code that a ColumnGenerator would require. I was hoping there was a slick way to avoid the custom code and just indicate which values from the array I wanted output for the table, but I guess there’s not…