Skip to content

Instantly share code, notes, and snippets.

@mstahv
Last active October 1, 2015 18:34
Show Gist options
  • Save mstahv/a687ca0b38fd93f6aee4 to your computer and use it in GitHub Desktop.
Save mstahv/a687ca0b38fd93f6aee4 to your computer and use it in GitHub Desktop.
Example to make text war in a Vaadin Table
package org.bluemix.mavenproject7;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.data.Property;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.ColumnGenerator;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import org.vaadin.viritin.fields.MTable;
import org.vaadin.viritin.label.RichText;
import org.vaadin.viritin.layouts.MVerticalLayout;
/**
* Making text wrap in a Vaadin Table.
*/
public class MyUI extends UI {
public static class Pojo {
private String myprop = "This is a loooong <strong>loooong</strong> loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong loooong text.";
public String getMyprop() {
return myprop;
}
public void setMyprop(String myprop) {
this.myprop = myprop;
}
}
@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new MVerticalLayout();
Pojo pojo = new Pojo();
Table table = new Table();
BeanItemContainer<Pojo> bic = new BeanItemContainer<>(Pojo.class);
bic.addBean(pojo);
table.setContainerDataSource(bic);
// this will replace the basic toString representation with a component
// we can easily configure
table.addGeneratedColumn("myprop", (Table source, Object itemId, Object columnId) -> {
String theValue = source.getContainerProperty(itemId, columnId).
getValue().toString();
// instead of plain text, lets return a Label with 100% width,
// also allow html formatting
Label label = new Label(theValue);
label.setContentMode(ContentMode.HTML);
label.setWidth("100%");
return label;
});
layout.addComponent(table);
// Same with Viritin (which you probably have on your classpath,
// if you started with Vaadin boilerplate)
MTable<Pojo> mTable = new MTable<>(Pojo.class).addBeans(pojo)
.withGeneratedColumn("myprop", p -> new RichText(p.getMyprop()));
layout.addComponent(mTable);
setContent(layout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment