简体   繁体   中英

How to create a table using tableviewer which has 2 coloumns and 10 rows in jface

How to create a table using TableViewer which has 2 columns and 10 rows.I am trying to create a table using the above link http://www.vogella.com/tutorials/EclipseJFaceTable/article.html

i am not able to develop it .So please help me to do as i am new to jface

I have created the columns of the table but i am not able to add the data into it using content provider and label so please help me to do.The code to create the column header is as follows

TableColumnLayout layout = new TableColumnLayout();
composite.setLayout(layout);

//Instantiate TableViewer
TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
table = tableViewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);

TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn empty = tableViewerColumn.getColumn();

layout.setColumnData(empty, new ColumnPixelData(100, true, true));
empty.setText("");

TableViewerColumn tableViewerColumn_Time = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn time = tableViewerColumn_Time.getColumn();
//Specify width using weights
layout.setColumnData(time, new ColumnWeightData(2, ColumnWeightData.MINIMUM_WIDTH, true));
time.setText("Time");

TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn message = tableViewerColumn_2.getColumn();
//Specify width using weights
layout.setColumnData(message, new ColumnWeightData(4, ColumnWeightData.MINIMUM_WIDTH, true));
message.setText("Message");

You need a class containing the data for the rows, something like:

class MyRowData
{
  String getTime()
  {
     ... your code
  }

  String getMessage()
  {
     .... your code
  }
}

Add column label providers to your columns which get the data from your class:

tableViewerColumn_Time.setLabelProvider(new ColumnLabelProvider() { 
  @Override
  public String getText(Object element) {
    return ((MyRowData)element).getTime();
  }
});

tableViewerColumn_2.setLabelProvider(new ColumnLabelProvider() { 
  @Override
  public String getText(Object element) {
    return ((MyRowData)element).getMessage();
  }
});

Use a standard array / list content provider:

tableViewer.setContentProvider(ArrayContentProvider.getInstance());

Create a list containing your rows:

List<MyRowData> rows = new ArrayList<>();

rows.add(new MyRowData(..... 
....

Tell the table viewer to use the list:

tableViewer.setInput(rows);

This setInput call must be done last.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM