Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How can I wrap TableViewer cells text?
How can I wrap TableViewer cells text? [message #709133] Wed, 03 August 2011 10:33 Go to next message
arthurav2005 is currently offline arthurav2005
Messages: 10
Registered: August 2011
Junior Member
I have a table in which some columns may be smaller than the text. I want the text to wrap on more rows. The default behavior in TableViewer with ColumnLabelProvider is to show ... instead of the remaining text. I have explicitly set SWT.WRAP on each column, but it doesn't do what I want.

The person class used:
package utilclass;

public class Person {
	public String first;
	public String second;
	public String gender;
	public Person(String firs, String sec, String gen){
		first=firs;
		second=sec;
		gender=gen;
	}
}



The code for the View is the following:

package test2_wrap;

import java.util.ArrayList;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

import utilclass.Person;

public class View extends ViewPart {
	public static final String ID = "test2_wrap.view";

	private TableViewer viewer;
	private Label label;
	private Button button;
	private GridData labelData;
	private ArrayList<Person> myCol;


	public void createPartControl(Composite parent) {
		myCol=new ArrayList<Person>();
		myCol.add(new Person("a1", "very loooooooong name","m"));
		myCol.add(new Person("a2", "y","n"));
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
		createColumns(parent, viewer);
		viewer.getTable().setLayout(new GridLayout(2,true));
		viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true ));
		viewer.getTable().setHeaderVisible(true);
		viewer.setContentProvider(new ArrayContentProvider());
		viewer.setInput(myCol);

	}
	
	private void createColumns(final Composite parent, final TableViewer viewer) {
		String[] titles = { "First name", "Last name", "Gender" };
		int[] bounds = { 100, 100, 100 };

		// First column is for the first name
		TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
		col.setLabelProvider(new ColumnLabelProvider() { 
			@Override
			public String getText(Object element) {
				Person p = (Person) element;
				return String.valueOf(p.first);
			}

		});
		
		col = createTableViewerColumn(titles[1], bounds[1], 1);

		col.setLabelProvider(new ColumnLabelProvider() { 
			@Override
			public String getText(Object element) {
				Person p = (Person) element;
				return String.valueOf(p.second);
			}});
		
		col = createTableViewerColumn(titles[2], bounds[2], 1);

		col.setLabelProvider(new ColumnLabelProvider() { 
			@Override
			public String getText(Object element) {
				Person p = (Person) element;
				return String.valueOf(p.gender);
			}});
	}
	
	private TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
				SWT.CENTER | SWT.WRAP);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(title);
		column.setWidth(bound);
		column.setResizable(true);
		column.setMoveable(true);
		return viewerColumn;

	}

	public void setFocus() {
		viewer.getControl().setFocus();
	}
}


Is there a way to change the labelprovider behaviour the "very looooooooong name" on multiple lines instead of diplaying ... ?

index.php/fa/3555/0/

Thank you

[Updated on: Wed, 03 August 2011 10:54]

Report message to a moderator

Re: Wrap TableViewer cells text [message #709193 is a reply to message #709133] Wed, 03 August 2011 12:02 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5262
Registered: July 2009
Senior Member
You need to use owner draw but even with that you are in trouble because
all rows in a table have to have the same height!

Tom
Am 03.08.11 16:33, schrieb arthurav2005:
> I have a table in which some columns may be smaller than the text. I want the text to wrap on more rows. The default behavior in TableViewer with ColumnLabelProvider is to show ... instead of the remaining text. I have explicitly set SWT.WRAP on each column, but it doesn't do what I want.
>
> The code for the View is the following:
>
>
> package test2_wrap;
>
> import java.util.ArrayList;
>
> import org.eclipse.jface.viewers.ArrayContentProvider;
> import org.eclipse.jface.viewers.ColumnLabelProvider;
> import org.eclipse.jface.viewers.TableViewer;
> import org.eclipse.jface.viewers.TableViewerColumn;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.widgets.TableColumn;
> import org.eclipse.ui.part.ViewPart;
>
> import utilclass.Person;
>
> public class View extends ViewPart {
> public static final String ID = "test2_wrap.view";
>
> private TableViewer viewer;
> private Label label;
> private Button button;
> private GridData labelData;
> private ArrayList<Person> myCol;
>
>
> public void createPartControl(Composite parent) {
> myCol=new ArrayList<Person>();
> myCol.add(new Person("a1", "very loooooooong name","m"));
> myCol.add(new Person("a2", "y","n"));
> viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
> | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
> createColumns(parent, viewer);
> viewer.getTable().setLayout(new GridLayout(2,true));
> viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true ));
> viewer.getTable().setHeaderVisible(true);
> viewer.setContentProvider(new ArrayContentProvider());
> viewer.setInput(myCol);
>
> }
>
> private void createColumns(final Composite parent, final TableViewer viewer) {
> String[] titles = { "First name", "Last name", "Gender" };
> int[] bounds = { 100, 100, 100 };
>
> // First column is for the first name
> TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
> col.setLabelProvider(new ColumnLabelProvider() {
> @Override
> public String getText(Object element) {
> Person p = (Person) element;
> return String.valueOf(p.first);
> }
>
> });
>
> col = createTableViewerColumn(titles[1], bounds[1], 1);
>
> col.setLabelProvider(new ColumnLabelProvider() {
> @Override
> public String getText(Object element) {
> Person p = (Person) element;
> return String.valueOf(p.second);
> }});
>
> col = createTableViewerColumn(titles[2], bounds[2], 1);
>
> col.setLabelProvider(new ColumnLabelProvider() {
> @Override
> public String getText(Object element) {
> Person p = (Person) element;
> return String.valueOf(p.gender);
> }});
> }
>
> private TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber) {
> final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
> SWT.CENTER | SWT.WRAP);
> final TableColumn column = viewerColumn.getColumn();
> column.setText(title);
> column.setWidth(bound);
> column.setResizable(true);
> column.setMoveable(true);
> return viewerColumn;
>
> }
>
> public void setFocus() {
> viewer.getControl().setFocus();
> }
> }
>
>
> Is there a way to change the labelprovider behaviour the "very looooooooong name" on multiple lines instead of diplaying ... ?
>
>
>
> Thank you
Re: Wrap TableViewer cells text [message #709718 is a reply to message #709193] Thu, 04 August 2011 04:16 Go to previous messageGo to next message
arthurav2005 is currently offline arthurav2005
Messages: 10
Registered: August 2011
Junior Member
Thank you. I have tried that and indeed there is a problem with row height. Is there a way of having variable row height with tableviewer/treeviewer? I have looked at the nebula gridtableviewer but it is not fast enough for my needs.
Re: Wrap TableViewer cells text [message #709726 is a reply to message #709718] Thu, 04 August 2011 04:20 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5262
Registered: July 2009
Senior Member
Am 04.08.11 10:16, schrieb arthurav2005:
> Thank you. I have tried that and indeed there is a problem with row
> height. Is there a way of having variable row height with

No.

> tableviewer/treeviewer? I have looked at the nebula gridtableviewer but
> it is not fast enough for my needs.

Really - as I'm the maintainer of it is Grid and we used it with big
datasets? Did you looked at NatTable?

Tom
Re: Wrap TableViewer cells text [message #709739 is a reply to message #709726] Thu, 04 August 2011 04:43 Go to previous messageGo to next message
arthurav2005 is currently offline arthurav2005
Messages: 10
Registered: August 2011
Junior Member
Does NatTable support trees? My first column is a tree.
Re: Wrap TableViewer cells text [message #709746 is a reply to message #709739] Thu, 04 August 2011 04:45 Go to previous message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5262
Registered: July 2009
Senior Member
Am 04.08.11 10:43, schrieb arthurav2005:
> Does NatTable support trees? My first column is a tree.

IIRC no but you'll have to ask at their forum? Might I reask what is
slow about Grid?

Tom
Previous Topic:Saving the state of a combo
Next Topic:expansion button in variables view not retaining the expanded state
Goto Forum:
  


Current Time: Fri Aug 01 22:43:24 EDT 2014

Powered by FUDForum. Page generated in 0.15559 seconds