Skip to main content



      Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Table editing using TableViewer
Table editing using TableViewer [message #460346] Wed, 20 December 2006 11:24 Go to next message
Eclipse UserFriend
Originally posted by: ssankhua.yahoo.com

Hi
I have a TableViewer and I am not able to edit the cells. Do I need to write listener separately. Please look at the code below

<b>My View -</b>
public void createPartControl(Composite parent)
{
final TableViewer tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION);
table = tableViewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
for (int i=0; i<columnNames.length; i++)
{
TableColumn column = new TableColumn(table, SWT.CENTER);
column.setText(columnNames[i]);
column.setWidth(100);
}

TextCellEditor[] cellEditors = new TextCellEditor[3];
for (int i = 0; i<3; i++)
cellEditors[i] = new TextCellEditor(table);
tableViewer.setCellEditors(cellEditors);

tableViewer.setCellModifier(new ExampleCellProvider());
tableViewer.setContentProvider(new BookListContentProvider());
tableViewer.setLabelProvider(new BookListLabelProvider());
tableViewer.setInput(new BookList());
}

<b>My Content Provider -</b>

public class BookListContentProvider implements IStructuredContentProvider
{
public Object[] getElements(Object inputElement){
return ((BookList)inputElement).getBookList();
}
public void dispose() {
}
public void inputChanged(Viewer v, Object o, Object n){ }
}

<b>My Label Provider -</b>

public class BookListLabelProvider extends LabelProvider implements ITableLabelProvider
{
public Image getColumnImage(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

public String getColumnText(Object element, int index) {
if (index == 0)
return ((Book)element).getTitleAsString();
else if (index == 1)
return ((Book)element).getAuthorAsString();
else
return ((Book)element).getQuantityAsString();
}
}

<b>My Cell Modifier -</b>
public class ExampleCellProvider implements ICellModifier
{
public boolean canModify(Object element, String property) {
return true;
}

public Object getValue(Object element, String property) {
//return ((Book) element).get(new Integer(property).intValue());
return null;
}

public void modify(Object element, String property, Object value) {
TableItem ti = (TableItem)element;
Book book = (Book)ti.getData();
//bean.set(new Integer(property).intValue(), (String)value);
}

}

<b>My Model -</b>
public class Book
{
private String title;
private String author;
private int quantity;

Book(String theTitle, String theAuthor, int theQuantity)
{
title = theTitle;
author = theAuthor;
quantity = theQuantity;
}
public String getTitleAsString()
{
return title;
}
public String getAuthorAsString()
{
return author;
}
public String getQuantityAsString()
{
return Integer.toString(quantity);
}
}

<b>My Model List -</b>
public class BookList
{
private Book books[] = new Book[6];
public BookList()
{
initializeBookList();
}

private void initializeBookList()
{
books[0] = new Book("Iliad", "Homer", 10);
books[1] = new Book("The Odyssey", "Homer", 9);
books[2] = new Book("Pride and Prejudice", "JaneAusten", 8);
books[3] = new Book("Sense and Sensibility", "JaneAusten", 7);
books[4] = new Book("The Europeans", "Henry James",6);
books[5] = new Book("Jane Eyre", "Charlotte Bronte",5);
}
public Book[] getBookList()
{
return books;
}
}

Please help.

Thanks
Barnali
Re: Table editing using TableViewer [message #460349 is a reply to message #460346] Wed, 20 December 2006 11:35 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

you missed calling setColumnProperties()!
For examples see the jface-snippet collection in cvs which you can
browse online using
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.s nippets/

Tom

barnali schrieb:
> Hi
> I have a TableViewer and I am not able to edit the cells. Do I need to write listener separately. Please look at the code below
>
> <b>My View -</b>
> public void createPartControl(Composite parent)
> {
> final TableViewer tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION);
> table = tableViewer.getTable();
> table.setHeaderVisible(true);
> table.setLinesVisible(true);
> for (int i=0; i<columnNames.length; i++)
> {
> TableColumn column = new TableColumn(table, SWT.CENTER);
> column.setText(columnNames[i]);
> column.setWidth(100);
> }
>
> TextCellEditor[] cellEditors = new TextCellEditor[3];
> for (int i = 0; i<3; i++)
> cellEditors[i] = new TextCellEditor(table);
> tableViewer.setCellEditors(cellEditors);
>
> tableViewer.setCellModifier(new ExampleCellProvider());
> tableViewer.setContentProvider(new BookListContentProvider());
> tableViewer.setLabelProvider(new BookListLabelProvider());
> tableViewer.setInput(new BookList());
> }
>
> <b>My Content Provider -</b>
>
> public class BookListContentProvider implements IStructuredContentProvider
> {
> public Object[] getElements(Object inputElement){
> return ((BookList)inputElement).getBookList();
> }
> public void dispose() {
> }
> public void inputChanged(Viewer v, Object o, Object n){ }
> }
>
> <b>My Label Provider -</b>
>
> public class BookListLabelProvider extends LabelProvider implements ITableLabelProvider
> {
> public Image getColumnImage(Object element, int columnIndex) {
> // TODO Auto-generated method stub
> return null;
> }
>
> public String getColumnText(Object element, int index) {
> if (index == 0)
> return ((Book)element).getTitleAsString();
> else if (index == 1)
> return ((Book)element).getAuthorAsString();
> else
> return ((Book)element).getQuantityAsString();
> }
> }
>
> <b>My Cell Modifier -</b>
> public class ExampleCellProvider implements ICellModifier
> {
> public boolean canModify(Object element, String property) {
> return true;
> }
>
> public Object getValue(Object element, String property) {
> //return ((Book) element).get(new Integer(property).intValue());
> return null;
> }
>
> public void modify(Object element, String property, Object value) {
> TableItem ti = (TableItem)element;
> Book book = (Book)ti.getData();
> //bean.set(new Integer(property).intValue(), (String)value);
> }
>
> }
>
> <b>My Model -</b>
> public class Book
> {
> private String title;
> private String author;
> private int quantity;
>
> Book(String theTitle, String theAuthor, int theQuantity)
> {
> title = theTitle;
> author = theAuthor;
> quantity = theQuantity;
> }
> public String getTitleAsString()
> {
> return title;
> }
> public String getAuthorAsString()
> {
> return author;
> }
> public String getQuantityAsString()
> {
> return Integer.toString(quantity);
> }
> }
>
> <b>My Model List -</b>
> public class BookList
> {
> private Book books[] = new Book[6];
> public BookList()
> {
> initializeBookList();
> }
>
> private void initializeBookList()
> {
> books[0] = new Book("Iliad", "Homer", 10);
> books[1] = new Book("The Odyssey", "Homer", 9);
> books[2] = new Book("Pride and Prejudice", "JaneAusten", 8);
> books[3] = new Book("Sense and Sensibility", "JaneAusten", 7);
> books[4] = new Book("The Europeans", "Henry James",6);
> books[5] = new Book("Jane Eyre", "Charlotte Bronte",5);
> }
> public Book[] getBookList()
> {
> return books;
> }
> }
>
> Please help.
>
> Thanks
> Barnali
Re: Table editing using TableViewer [message #460351 is a reply to message #460349] Wed, 20 December 2006 11:53 Go to previous messageGo to next message
Eclipse UserFriend
Tom Schindl a écrit :
> Hi,
>
> you missed calling setColumnProperties()!
> For examples see the jface-snippet collection in cvs which you can
> browse online using
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.s nippets/

Hi,
It's too bad I didn't know about these snippets when I struggled hard to
implement table editing some time ago...
It would surely be more useful to have these grouped in a web page, like
www.eclipse.org/swt/snippets/

--
Patrick
Re: Table editing using TableViewer [message #460353 is a reply to message #460351] Wed, 20 December 2006 12:00 Go to previous messageGo to next message
Eclipse UserFriend
Hi Patrick,

When I have time I'll start a section in the wiki to present all those
snippets to the public. I think this is the best place because JFace
doesn't have a public site. Feel free to start the wiki integration
yourself ;-)

Tom

Patrick Godeau schrieb:
> Tom Schindl a écrit :
>> Hi,
>>
>> you missed calling setColumnProperties()!
>> For examples see the jface-snippet collection in cvs which you can
>> browse online using
>> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.s nippets/
>
> Hi,
> It's too bad I didn't know about these snippets when I struggled hard to
> implement table editing some time ago...
> It would surely be more useful to have these grouped in a web page, like
> www.eclipse.org/swt/snippets/
>
Re: Table editing using TableViewer [message #460468 is a reply to message #460349] Thu, 21 December 2006 04:31 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ssankhua.yahoo.com

Thanks a lot.. it works :)

On editing the value is set in the model, but it is not visible in the UI, I will look into that.
Re: Table editing using TableViewer [message #460477 is a reply to message #460468] Thu, 21 December 2006 06:27 Go to previous message
Eclipse UserFriend
you need to call viewer.update(item.getData(),true);

Tom

barnali schrieb:
> Thanks a lot.. it works :)
>
> On editing the value is set in the model, but it is not visible in the UI, I will look into that.
Previous Topic:Singleton Editor or View?
Next Topic:Exception launching the Eclipse Platform
Goto Forum:
  


Current Time: Mon Mar 17 20:32:54 EDT 2025

Powered by FUDForum. Page generated in 0.02882 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top