I have created a TableViewer with two columns and I want to add rows to it after pushing a button called "add". After pushing the button I want to create an empty row which first column cell will give the option to type text and the second column cell will give the option to browse in the system directories to select a folder.
I have managed to add an empty row after clicking the "add" button, but it returns the following error: An error has occurred. See error log for more details. assertion failed: Column 0 has no label provider. The purpose of the tableviewer is to insert a String variable in the first column cell and a path in the second column cell. With the data inserted by the user I want to create a List of a class called ProjectDirectory (this class has two String attributes).
I have also managed to remove the rows I select with the mouse and this works fine.
I am newbie using Jface and SWT, and I can´t get the way to do this. Can anyone help me please?
Code:
Composite tableComposite = new Composite(pluginPathCreatedComposite, SWT.NONE);
FormData tableCompositeData = new FormData();
tableCompositeData.left = new FormAttachment(selectProjectPathLabel, 0, SWT.LEFT);
tableCompositeData.top = new FormAttachment(selectProjectPathLabel, 40, SWT.TOP);
tableCompositeData.right = new FormAttachment(selectProjectPathButton,-10,SWT.LEFT);
tableCompositeData.bottom = new FormAttachment(createProjectConfigButton,-10,SWT.TOP);
tableComposite.setLayoutData(tableCompositeData);
//Create the tableviewer
tableViewer = new TableViewer(tableComposite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
Table table = tableViewer.getTable();
tableViewer.getTable().setLinesVisible(true);
tableViewer.getTable().setHeaderVisible(true);
tableViewer.setColumnProperties(COLUMN_HEADINGS);
//Create the tableViewer columns
TableViewerColumn variable = new TableViewerColumn(tableViewer, SWT.NONE);
variable.getColumn().setText(COLUMN_HEADINGS[0]);
variable.getColumn().setResizable(false);
TableViewerColumn path = new TableViewerColumn(tableViewer, SWT.NONE);
path.getColumn().setText(COLUMN_HEADINGS[1]);
path.getColumn().setResizable(false);
TableColumnLayout tableLayout = new TableColumnLayout();
tableComposite.setLayout(tableLayout);
//Set the columns proportions
tableLayout.setColumnData(path.getColumn(), new ColumnWeightData(50));
tableLayout.setColumnData(variable.getColumn(), new ColumnWeightData(50));
//Set the first row as TextCell (this doesn´t work)
CellEditor[] editors = new CellEditor[2];
editors[0] = new TextCellEditor(tableViewer.getTable());
editors[1] = new TextCellEditor(tableViewer.getTable());
tableViewer.setCellEditors(editors);
//Add button
Button addRow = new Button(pluginPathCreatedComposite, SWT.NONE);
FormData addRowData = new FormData();
addRowData.top = new FormAttachment(tableComposite,0,SWT.TOP);
addRowData.left = new FormAttachment(selectProjectPathButton,0,SWT.LEFT);
addRowData.right = new FormAttachment(selectProjectPathButton,0,SWT.RIGHT);
addRow.setLayoutData(addRowData);
addRow.setText("Add");
addRow.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ProjectDirectory directory = new ProjectDirectory("A", "A");
Table table = tableViewer.getTable();
tableViewer.add(directory);
table.setTopIndex(table.getItemCount());
table.select(table.getItemCount() - 1);
}
});
//Delete row
Button deleteRow = new Button(pluginPathCreatedComposite, SWT.NONE);
FormData deleteRowData = new FormData();
deleteRowData.top = new FormAttachment(addRow,5,SWT.BOTTOM);
deleteRowData.left = new FormAttachment(selectProjectPathButton,0,SWT.LEFT);
deleteRowData.right = new FormAttachment(selectProjectPathButton,0,SWT.RIGHT);
deleteRow.setLayoutData(deleteRowData);
deleteRow.setText("Delete");
deleteRow.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ISelection selection = tableViewer.getSelection();
if (selection instanceof IStructuredSelection) {
Iterator iterator = ((IStructuredSelection) selection).iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
tableViewer.remove(obj);
}
}
}
});
Image:
EDIT:
I have created a content provider to take the ProjectDirectories of a List created in the code (I will change this in the future). After that, I have created a label provider for each column and it displays the data like I want.
The fact is that I want to add a new row in the table after pushing the add button and I want to make the first cell be a TextCellEditor (the second one will have an event to open the directory path of the system as the image one, but before doing this I also want to make it TextCellEditor). I have defined that the two columns will be like TextCell editor, and after pushing the add button I have managed to add an empty row, but I can´t get to type text in them. I have also created a setCellModifier to update the tableviewer. What am I doing wrong?
Code
Composite tableComposite = new Composite(pluginPathCreatedComposite, SWT.NONE);
FormData tableCompositeData = new FormData();
tableCompositeData.left = new FormAttachment(selectProjectPathLabel, 0, SWT.LEFT);
tableCompositeData.top = new FormAttachment(selectProjectPathLabel, 40, SWT.TOP);
tableCompositeData.right = new FormAttachment(selectProjectPathButton,-10,SWT.LEFT);
tableCompositeData.bottom = new FormAttachment(createProjectConfigButton,-10,SWT.TOP);
tableComposite.setLayoutData(tableCompositeData);
tableViewer = new TableViewer(tableComposite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
table = tableViewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(true);
tableViewer.setColumnProperties(COLUMN_HEADINGS);
//Content provider
tableViewer.setContentProvider(new ProjectConfigTableContentProvider());
/*I have tried to use this instead of setting the label providers in the column definitions*/
//tableViewer.setLabelProvider(new ProjectConfigTableLabelProvider());
//Label provider setted in each column
TableViewerColumn variable = new TableViewerColumn(tableViewer, SWT.NONE);
variable.getColumn().setText(COLUMN_HEADINGS[0]);
variable.getColumn().setResizable(false);
variable.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element) {
if(element instanceof ProjectDirectory && element != null)
return ((ProjectDirectory)element).getName();
return super.getText(element);
}
});
TableViewerColumn path = new TableViewerColumn(tableViewer, SWT.NONE);
path.getColumn().setText(COLUMN_HEADINGS[1]);
path.getColumn().setResizable(false);
path.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element) {
if(element instanceof ProjectDirectory && element != null)
return ((ProjectDirectory)element).getPath();
return super.getText(element);
}
});
//List created in code (will change this in the future)
List<ProjectDirectory> b = new ArrayList<ProjectDirectory>();
b.add(new ProjectDirectory("AA", "FF"));
b.add(new ProjectDirectory("BB", "CC"));
Project pro = new Project("A", "B", 2, b);
//Input setted
tableViewer.setInput(pro);
TableColumnLayout tableLayout = new TableColumnLayout();
tableComposite.setLayout(tableLayout);
tableLayout.setColumnData(path.getColumn(), new ColumnWeightData(50));
tableLayout.setColumnData(variable.getColumn(), new ColumnWeightData(50));
CellEditor[] editors = new CellEditor[2];
editors[0] = new TextCellEditor(table);
editors[1] = new TextCellEditor(table);
tableViewer.setCellEditors(editors);
tableViewer.setCellModifier(new ProjectConfigTableCellModifier(COLUMN_HEADINGS,tableViewer));
//addrow button
Button addRow = new Button(pluginPathCreatedComposite, SWT.NONE);
FormData addRowData = new FormData();
addRowData.top = new FormAttachment(tableComposite,0,SWT.TOP);
addRowData.left = new FormAttachment(selectProjectPathButton,0,SWT.LEFT);
addRowData.right = new FormAttachment(selectProjectPathButton,0,SWT.RIGHT);
addRow.setLayoutData(addRowData);
addRow.setText("Add");
addRow.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ProjectDirectory directory = new ProjectDirectory("", "");
tableViewer.add(directory);
table.setTopIndex(table.getItemCount());
table.select(table.getItemCount() - 1);
tableViewer.editElement(directory, 0);
}
});
I also have created a labelProvider class to avoid setting the labels providers in each column like I have done, but it doesn´t work. The code is this:
@Override
public String getColumnText(Object element, int columnIndex) {
// TODO Auto-generated method stub
ProjectDirectory projectDirectory = (ProjectDirectory) element;
switch (columnIndex) {
case 0:
return projectDirectory.getName();
case 1:
return projectDirectory.getPath();
}
return null;
}
And the call to the class is like this:
tableViewer.setLabelProvider(new ProjectConfigTableLabelProvider());
Why does only work setting the label providers to the column and this way doen´t work?
setInputon the viewer, this is also required. To add / remove things you update the 'model' data in the content provider and then tell the viewer about these changes. - greg-449TableViewerColumn.setEditingSupportto set an editing support class for each column is the recommended way to do tables these days. - greg-449