CODEXE

Add button and label to table

add button and label to table

The label can use to show the different color. The button is located on the right side while the left area can show other contents.

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.setLabelProvider(new ColumnLabelProvider(){
	@Override
	public void update(ViewerCell cell) {
		TableItem item = (TableItem) cell.getItem();
		Label label = new Label((Composite)cell.getControl(),SWT.NONE);
		label.setBackground(new Color(null, 255,100,120));		       
			TableEditor editor = new TableEditor(item.getParent());
			editor.grabHorizontal  = true;
			editor.grabVertical = true;
		editor.setEditor(label, item, cell.getColumnIndex());
		editor.layout();    
	}
	 @Override
		 public String getText(Object element){
				if(element instanceof TableViewerInput){
					   return ((TableViewerInput)element).getColumn1();
				 }
				 return super.getText(element);
			 }
});

TableViewerColumn viewerCol2 = new TableViewerColumn(tableViewer,SWT.NONE);
TableColumn col2 = viewerCol2.getColumn();
col2.setText("Column 2");
col2.setWidth(100);
viewerCol2.setLabelProvider(new ColumnLabelProvider(){
			
	@Override
	public void update(ViewerCell cell) {
		TableItem item = (TableItem) cell.getItem();
		Button btn = new Button((Composite)cell.getControl(),SWT.NONE);
		btn.setText("...");
			   
		TableEditor editor = new TableEditor(item.getParent());
	   //   editor.grabHorizontal  = true;
		editor.horizontalAlignment = SWT.RIGHT;
		editor.minimumWidth = btn.getSize().x + 20;
		editor.setEditor(btn, item, cell.getColumnIndex());
		editor.layout();    
	}
	@Override
		public String getText(Object element){
				if(element instanceof TableViewerInput){
					   return ((TableViewerInput)element).getColumn2();
				 }
				 return super.getText(element);
		}
});
tableViewer.setContentProvider(new TableViewerContentProvider());
tableViewer.add(new TableViewerInput("col1_1","combo1"));
			 

Tips:

The implementation can be extended to add listener to the button and show the color dialog to change the color of the label