CODEXE

Create a table

swt jface table

1. SWT Table

Table table = new Table(container, SWT.NONE);
TableColumn col1 = new TableColumn(table, SWT.NONE);
col1.setText("Column 1");//column title
col1.setWidth(100);//column width
TableColumn col2 = new TableColumn(table,SWT.NONE);
col2.setText("Column 2");
col2.setWidth(100);
TableItem item1 = new TableItem(table,SWT.NONE);
item1.setText(new String[]{"col1_1","col2_1"});
TableItem item2 = new TableItem(table,SWT.NONE);
item2.setText(new String[]{"col2_1","col2_2"});
table.setHeaderVisible(true);
table.setLinesVisible(true);
			 

2. JFace TableViewer

public class TableViewerInput{
				 private String col1;
	 private String col2;
	 public TableViewerInput(String col1, String col2){
		 this.col1 = col1;
		 this.col2 = col2;
	 }
	 public void setColumn1(String col1){
		 this.col1 = col1;
	 }
	 public void setColumn2(String col2){
		 this.col2 = col2;
	 }
	 public String getColumn1(){
		 return this.col1;
	 }
	 public String getColumn2(){
		 return this.col2;
	 }
 }
			 
public class TableViewerContentProvider implements IStructuredContentProvider{
	   @Override
	   public void dispose() {
					   // TODO Auto-generated method stub
					   
	   }

	   @Override
	   public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
					   // TODO Auto-generated method stub           
	   }

	   @Override
	   public Object[] getElements(Object inputElement) {
					   // TODO Auto-generated method stub
					   if(inputElement instanceof TableViewerInput){
									   return new Object[]{inputElement};
					   }
					   
					   return null;
	   }
 }
			 
 public class TableViewerLabelProvider extends LabelProvider implements ITableLabelProvider{

	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getColumnText(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		if(element instanceof TableViewerInput){
			TableViewerInput input  = (TableViewerInput)element;
			switch(columnIndex){
			case 0:	
				return input.getColumn1();
			case 1:
				return input.getColumn2();
			}
			
		}
		return null;
	}
	 
 }
			 
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);
TableViewerColumn viewerCol2 = new TableViewerColumn(tableViewer,SWT.NONE);
TableColumn col2 = viewerCol2.getColumn();
col2.setText("Column 2");
col2.setWidth(100);
tableViewer.setContentProvider(new TableViewerContentProvider());
tableViewer.setLabelProvider(new TableViewerLabelProvider());
		
tableViewer.add(new TableViewerInput("col1_1","combo1"));
tableViewer.add(new TableViewerInput("col2_1","combo2"));
			 

Tips:

Add a row:

tableViewer.add(element)

Remove a row:

tableViewer.remove(element)

Get selected row:

Object element = tableViewer.getStructuredSelection().getFirstElement()