0

I am implementing table viewer like this

folderAssociationTable = componentsRenderer.createTableWidget(container, SWT.BORDER | SWT.MULTI  | SWT.FULL_SELECTION, 2, true);

        folderAssociationTable.addListener(SWT.MeasureItem, new Listener() {
            public void handleEvent(Event event) {
                event.height = 20;
            }
        });
        tableViewer  = new TableViewer(folderAssociationTable);
        tableViewer.getTable().setHeaderVisible(true);
        tableViewer.getTable().setLinesVisible(true);
        tableViewer.setContentProvider(new ArrayContentProvider());


        TableColumn column = new TableColumn(tableViewer.getTable(), SWT.NONE);
        column.setText("CheckBox");
        column.setWidth(100);
        checkBoxColumn = new TableViewerColumn(tableViewer, column);


        column = new TableColumn(tableViewer.getTable(), SWT.NONE);
        column.setText("Source Folder");
        column.setWidth(200);
        sourceFolderColumn = new TableViewerColumn(tableViewer, column);

        column = new TableColumn(tableViewer.getTable(), SWT.NONE);
        column.setText("Target Folder");
        column.setWidth(200);
        targetFolderColumn = new TableViewerColumn(tableViewer, column);

now i want to add conent to this table , first column i want to add checkbox, and in another two columns want to add string values dynamically in for loop , how can i add these content to Table Viewer .

Baz
  • 34,567
  • 11
  • 66
  • 88
mayur_mitkari
  • 149
  • 5
  • 13

1 Answers1

0

First, if you just want to show checkboxes, strings and images which you set, you don't need TableViewer. Use a Table instead! Viewers are useful if you need something more complex, such as showing arbitrary Java objects.

  1. If you want to add checkboxes, use CheckboxTableViewer and an ICheckStateProvider. You'll need to create your table with SWT.CHECK style.
  2. To add values, you need to set some input for your viewer. In case of ArrayContentProvider, this is just an array or a collection:

    // ModelObject is whatever type of objects you want to show. Maybe just String...
    List<ModelObject> inputList = new ArrayList<ModelObject>();
    // build the list
    while (...) {
        ModelObject newObject = ...
        inputList.add(newObject);
    }
    
    tableViewer.setInput(inputList);
    

    If you need to change the list later, call tableViewer.refresh().

For the rest, see Vogella tutorial.

Alexey Romanov
  • 154,018
  • 31
  • 276
  • 433
  • Hello Alexey , I have a table inside which i am implementing to table viewer , one for checkbox ir, CheckBoxTableViewer and another one normal TableViewer For String Component, so On the Basis of entries of string component i want to add checkboxes , how can i do that – mayur_mitkari Feb 06 '13 at 05:42
  • No, you should never have two table viewers for one table, it doesn't make sense. – Alexey Romanov Feb 06 '13 at 07:01
  • so alexey , how can i handle this situation in my case i want to add two simple strings to two columns and check box in that row based on the count i wnat to do these things dynamically , the solution you suggested above i have tried it but when i was taking string array like this String[] folderInfo = {folderlist[i],targetFolderPath1}; tableViewer.setInput(folderInfo); the folderlist[i] and targetfolder path was getting added in first column itself which is meant for checkbox , so please elaborate on this situation and how can i add checkbox in 1st col – mayur_mitkari Feb 06 '13 at 07:19
  • and in case of simple swt Table , i am facing this issue that why i implemented TableViewer http://stackoverflow.com/questions/14701116/refreshing-table-editor-with-table-item-in-swt – mayur_mitkari Feb 06 '13 at 07:22
  • First, checkbox will appear automatically if you add `SWT.CHECK`. If you are using `ArrayContentProvider`, every element of your input goes into a separate row. You'll need to do something like `String[][] folderInfo = new String[] { new String[] {folderlist[i],targetFolderPath1} }` and add a `TableLabelProvider` which takes `String[]` as elements. – Alexey Romanov Feb 06 '13 at 11:35