Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Adding POJOs to Table using formatting and sorting still works

Jani Laakso
1 decade ago Mar 30, 2010 8:36am

There has been lots of questions regarding how to add POJOs into a Table when changing the presentation values for Property objects without breaking Table's sort functionality.

How about doing it the simple way in which you have full control to decide what and how you put into your table's container.

Below example takes List of POJOs and decides which one of the POJOs fields are displayed and how they are displayed. NumericString class is used to change the presentation to use correct Locale when formatting monetary values, add dollar symbol and two two fractiondigits, but the actual value for the Vaadin Property instances are unchanged, hence sorting works correctly.

About the other options:
1) Use BeanItemContainer, but with that you lose the possibility to change how Property values are presented or sorting works wrong.
2) Use Table.addGeneratedColumn but with this you lose sorting capability.
3) Override Table.getPropertyValue() and table.getType(Object propertyId), haven't actually tried this one myself but this just feels wrong way to do it

/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.sandbox;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;

import com.sandbox.common.DataMockup;
import com.sandbox.common.User;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Table;

public class SimpleTableExample extends CustomComponent {

	final static NumberFormat formatter = NumberFormat.getNumberInstance();

	public SimpleTableExample() {

		formatter.setMaximumFractionDigits(2);
		formatter.setMinimumFractionDigits(2);

		final Table table = new Table();

		// Define container properties
		table.addContainerProperty("Full name", String.class, null);
		table.addContainerProperty("email", String.class, null);
		table.addContainerProperty("accountBalance", NumericString.class, null);

		// Get all user POJOs
		List<User> users = DataMockup.getAllUsers();
		// Add POJOs to container
		for (User user : users) {
			String fullname = user.getFirstname() + " " + user.getLastname();
			String email = user.getEmail();
			// Change presentation value without breaking table sorting
			NumericString accountBalance = new NumericString(user
					.getAccountBalance(), formatter);
			// Create item, use user as id
			Object[] row = new Object[] { fullname, email, accountBalance };
			table.addItem(row, user);
		}

		// Define visible columns for table
		table.setVisibleColumns(new Object[] { "Full name", "email",
				"accountBalance" });

		table.setSizeFull();
		setCompositionRoot(table);
	}

	/**
	 * Wrapper class for a BigDecimal to be used in Vaadin Table. Presentation
	 * value does not affect the actual value which is used to sort table items.
	 * <p>
	 * Uses {@link Comparable} and equality methods from {@link BigDecimal}, and
	 * toString uses the formatter (or BigDecimal's own if null).
	 * </p>
	 */
	public class NumericString implements Comparable<NumericString> {

		private final BigDecimal value;
		private final NumberFormat formatter;

		public NumericString(final BigDecimal value,
				final NumberFormat formatter) {
			this.value = value;
			this.formatter = formatter;
		}

		public int compareTo(final NumericString o) {
			return value.compareTo(o.value);
		}

		@Override
		public int hashCode() {
			return value.hashCode();
		}

		@Override
		public boolean equals(final Object o) {
			return value.equals(o);
		}

		@Override
		public String toString() {
			if (formatter == null) {
				return value.toString();
			}
			return "$" + formatter.format(value);
		}
	}

}
Last updated on Mar, 30th 2010
Jani Laakso
1 decade ago Mar 30, 2010 9:28am
member sound
8 years ago Aug 12, 2013 2:05pm
Henri Sara
8 years ago Aug 13, 2013 5:38am
member sound
8 years ago Aug 13, 2013 8:35am
Henri Sara
8 years ago Aug 13, 2013 9:06am