CODEXE

Add text and combo box to table

add text to table add combo box to table

Both the functions will be implemented through adding editing support.

All codes are based on the JFace table viewer in Create A Table. The first column is editable and the second column is a combo box.

Editing Support

public class TableViewerEditingSupport extends EditingSupport{
	private CellEditor editor;
	private TableViewer tableviewer;
	private int column;
	public TableViewerEditingSupport(ColumnViewer viewer, int column) {
		super(viewer);
		// TODO Auto-generated constructor stub
		this.tableviewer = (TableViewer) viewer;
		switch(column){
		case 0:
			editor = new TextCellEditor(tableviewer.getTable());//first column
			break;
		case 1:
			editor = new ComboBoxCellEditor(tableviewer.getTable(), new String[]{"combo1","combo2"});//second column; contents are combo and combo2
			break;
		}
		this.column = column;
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		// TODO Auto-generated method stub
		return editor;
	}

	@Override
	protected boolean canEdit(Object element) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected Object getValue(Object element) {
		// TODO Auto-generated method stub
		if(element instanceof TableViewerInput){
			TableViewerInput input = (TableViewerInput)element;
			switch(column){
			case 0:
				return input.getColumn1();
			case 1:
				//return corresponding index in combobox
				if("combo1".equals(input.getColumn2())){
					return 0;
				}
				else if("combo2".equals(input.getColumn2())){
					return 1;
				}
			}
		}
		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		// TODO Auto-generated method stub
		 if(element instanceof TableViewerInput){
			 TableViewerInput input = (TableViewerInput)element;
			 switch(column){
			 case 0:
				 input.setColumn1(value.toString());
				 break;
			 case 1:
				 int index = (Integer)value;
				 if(index == 0){
					 input.setColumn2("combo1");
				 }
				 else if(index == 1){
					 input.setColumn2("combo2");
				 }
				 break;
			 }
			 
			 tableviewer.update(element, null);
		 }
	}
 }
			 

Add editing support to table viewer

TableViewer tableViewer = new TableViewer(container,SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
Table table = tableViewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableViewerColumn viewerCol1 = new TableViewerColumn(tableViewer,SWT.NONE);
TableColumn col1 = viewerCol1.getColumn();
col1.setText("Column 1");
col1.setWidth(100);
viewerCol1.setEditingSupport(new TableViewerEditingSupport(tableViewer, 0));//editable text
TableViewerColumn viewerCol2 = new TableViewerColumn(tableViewer,SWT.NONE);
TableColumn col2 = viewerCol2.getColumn();
col2.setText("Column 2");
col2.setWidth(100);
viewerCol2.setEditingSupport(new TableViewerEditingSupport(tableViewer, 1));//combobox
tableViewer.setContentProvider(new TableViewerContentProvider());
tableViewer.setLabelProvider(new TableViewerLabelProvider());
		
tableViewer.add(new TableViewerInput("col1_1","combo1"));
tableViewer.add(new TableViewerInput("col2_1","combo2"));