Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Simple table editor
Simple table editor [message #458254] Tue, 12 July 2005 08:10 Go to next message
Eclipse UserFriend
Originally posted by: prokopiev.stc.donpac.ru

Hi,

What's wrong in this simple table editor?

public class TestAppTableViewer extends ApplicationWindow {

public TestAppTableViewer() {
super(null);
}

protected Control createContents(Composite parent) {

Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new FillLayout());
TableViewer viewer = new TableViewer(container, SWT.BORDER |
SWT.V_SCROLL | SWT.H_SCROLL);
Table table = (Table)viewer.getControl();
table.setHeaderVisible(true);
TableColumn column = new TableColumn(table, SWT.LEFT);
column.setText("Name");
column.setWidth(180);
column = new TableColumn(table, SWT.LEFT);
column.setText("Value");
column.setWidth(80);
viewer.setContentProvider(new ArrayContentProvider());
viewer.setLabelProvider(new MyLabelProvider());
viewer.setCellEditors(new CellEditor[] {
new TextCellEditor(),
new TextCellEditor()});
viewer.setCellModifier(new MyCellModifier(viewer));
viewer.setInput(new Data[] {
new Data("Name 1", "Value 1"),
new Data("Name 2", "Value 2"),
new Data("Name 3", "Value 3")
});
return parent;
}

public static void main(String[] args) throws IOException {
TestAppTableViewer w = new TestAppTableViewer();
w.setBlockOnOpen(true);
w.open();
}

private class MyLabelProvider extends LabelProvider implements
ITableLabelProvider {

public Image getColumnImage (Object element, int columnIndex) {
return null;
}

public String getColumnText (Object element, int columnIndex) {
Data item = (Data)element;
switch (columnIndex) {
case 0: return item.getName();
case 1: return item.getValue();
}
return "";
}
}

private class MyCellModifier implements ICellModifier {

private Viewer viewer;

public MyCellModifier(Viewer viewer) {
this.viewer = viewer;
}

public boolean canModify(Object element, String property) {
return true;
}

public Object getValue(Object element, String property) {
Data item = (Data)element;
if (property.equals("Name"))
return item.getName();
else if (property.equals("Value"))
return item.getValue();
else
return null;
}

public void modify(Object element, String property, Object changed) {
Data item = (Data)element;
if (property.equals("Name"))
item.setName(changed.toString());
else if (property.equals("Value"))
item.setValue(changed.toString());
viewer.refresh();
}
}

private class Data {

private String name;
private String value;

public Data(String name, String value) {
this.name = name;
this.value = value;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

}

On cell click I got:

java.lang.NullPointerException
at
org.eclipse.jface.viewers.TableEditorImpl.activateCellEditor (TableEditorImpl.java:72)
at
org.eclipse.jface.viewers.TableEditorImpl.activateCellEditor (TableEditorImpl.java:145)
at
org.eclipse.jface.viewers.TableEditorImpl.handleMouseDown(Ta bleEditorImpl.java:271)
at org.eclipse.jface.viewers.TableViewer$2.mouseDown(TableViewe r.java:608)
at
org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:133)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1021)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:2867)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :2572)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:809 )
at org.eclipse.jface.window.Window.open(Window.java:787)
at testapp.TestAppTableViewer.main(TestAppTableViewer.java:76)
Re: Simple table editor [message #458272 is a reply to message #458254] Tue, 12 July 2005 14:48 Go to previous message
Eclipse UserFriend
Originally posted by: raghu.unikensystems.com

One thing is that the CellEditors need to be explicitly created with a
composite parent reference
as in
// create the cell editors and assoc. with viewer
CellEditor[] ces = new CellEditor[] {
new TextCellEditor(),
new TextCellEditor()};

for(int i=0;i<ces.length;i++) ces[i].create(parent);

viewer.setCellEditors(ces);

.....

in this case however, the selection yields a TableItem to the
ICellModifier.modify()
where u'll need to do something like
Data item = (Data)((TableItem)element).getData();


hth,
Raghu


"Eugene Prokopiev" <prokopiev@stc.donpac.ru> wrote in message
news:davtvl$c4c$1@news.eclipse.org...
> Hi,
>
> What's wrong in this simple table editor?
>
> public class TestAppTableViewer extends ApplicationWindow {
>
> public TestAppTableViewer() {
> super(null);
> }
>
> protected Control createContents(Composite parent) {
> Composite container = new Composite(parent, SWT.NONE);
> container.setLayout(new FillLayout());
> TableViewer viewer = new TableViewer(container, SWT.BORDER | SWT.V_SCROLL
> | SWT.H_SCROLL);
> Table table = (Table)viewer.getControl();
> table.setHeaderVisible(true);
> TableColumn column = new TableColumn(table, SWT.LEFT);
> column.setText("Name");
> column.setWidth(180);
> column = new TableColumn(table, SWT.LEFT);
> column.setText("Value");
> column.setWidth(80);
> viewer.setContentProvider(new ArrayContentProvider());
> viewer.setLabelProvider(new MyLabelProvider());
> viewer.setCellEditors(new CellEditor[] {
> new TextCellEditor(),
> new TextCellEditor()});
> viewer.setCellModifier(new MyCellModifier(viewer));
> viewer.setInput(new Data[] {
> new Data("Name 1", "Value 1"),
> new Data("Name 2", "Value 2"),
> new Data("Name 3", "Value 3")
> });
> return parent;
> }
>
> public static void main(String[] args) throws IOException {
> TestAppTableViewer w = new TestAppTableViewer();
> w.setBlockOnOpen(true); w.open();
> }
>
> private class MyLabelProvider extends LabelProvider implements
> ITableLabelProvider {
>
> public Image getColumnImage (Object element, int columnIndex) {
> return null;
> }
>
> public String getColumnText (Object element, int columnIndex) {
> Data item = (Data)element;
> switch (columnIndex) {
> case 0: return item.getName();
> case 1: return item.getValue();
> }
> return "";
> }
> }
>
> private class MyCellModifier implements ICellModifier {
>
> private Viewer viewer;
>
> public MyCellModifier(Viewer viewer) {
> this.viewer = viewer;
> }
>
> public boolean canModify(Object element, String property) {
> return true;
> }
>
> public Object getValue(Object element, String property) {
> Data item = (Data)element;
> if (property.equals("Name"))
> return item.getName();
> else if (property.equals("Value"))
> return item.getValue();
> else
> return null;
> }
>
> public void modify(Object element, String property, Object changed) {
> Data item = (Data)element;
> if (property.equals("Name"))
> item.setName(changed.toString());
> else if (property.equals("Value"))
> item.setValue(changed.toString());
> viewer.refresh();
> } }
>
> private class Data {
>
> private String name;
> private String value;
>
> public Data(String name, String value) {
> this.name = name;
> this.value = value;
> }
>
> public String getValue() {
> return value;
> }
>
> public void setValue(String value) {
> this.value = value;
> }
>
> public String getName() {
> return name;
> }
>
> public void setName(String name) {
> this.name = name;
> }
> }
>
> }
>
> On cell click I got:
>
> java.lang.NullPointerException
> at
> org.eclipse.jface.viewers.TableEditorImpl.activateCellEditor (TableEditorImpl.java:72)
> at
> org.eclipse.jface.viewers.TableEditorImpl.activateCellEditor (TableEditorImpl.java:145)
> at
> org.eclipse.jface.viewers.TableEditorImpl.handleMouseDown(Ta bleEditorImpl.java:271)
> at org.eclipse.jface.viewers.TableViewer$2.mouseDown(TableViewe r.java:608)
> at
> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:133)
> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1021)
> at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:2867)
> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :2572)
> at org.eclipse.jface.window.Window.runEventLoop(Window.java:809 )
> at org.eclipse.jface.window.Window.open(Window.java:787)
> at testapp.TestAppTableViewer.main(TestAppTableViewer.java:76)
Previous Topic:Cairo rectangle clipping causing JVM crash
Next Topic:help with TableTreeViewer
Goto Forum:
  


Current Time: Tue Oct 20 03:13:28 GMT 2020

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

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

Back to the top