简体   繁体   中英

How to make TableViewer exapandable in SWT?

I made

class ViewContentProvider implements IStructuredContentProvider 

which provides hierarchical data, also I made appropriate

class ViewLabelProvider extends LabelProvider implements ITableLabelProvider

Then I wrote

public void createPartControl(Composite parent) {
        viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
                | SWT.V_SCROLL);
        viewer.setContentProvider(new ViewContentProvider());
        viewer.setLabelProvider(new ViewLabelProvider());
        // Provide the input to the ContentProvider

        viewer.setInput(...

Unfortunately, view displays only the members of topmost node. It does not display topmost node itself. Also, the more nested childs are not displayed, ie all displayed nodes displayed as childless.

Why? How to make TableView look like is is drawn everywhere as table with expandable nodes?

If you want to display nodes with parent-child relationship, then you should be using org.eclipse.jface.viewers.TreeViewer .

If the information for the node has to be displayed with multiple columns like a table, then you can use the org.eclipse.jface.viewers.TreeViewerColumn .

Hope this helps,

    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new BorderLayout(0, 0));

    Group grp = new Group(composite, SWT.NONE);
    grp.setText(Messages.get().getValue("Some_text"));
    grp.setLayout(new FillLayout(SWT.HORIZONTAL));

    tree = new Tree(grp, SWT.BORDER);
    tree.setLinesVisible(true);

    TreeColumn trclmnNewColumn = new TreeColumn(tree, SWT.NONE);
    trclmnNewColumn.setWidth(100);
    trclmnNewColumn.setText(Messages.get().getValue("Type"));

    TreeColumn trclmnNewColumn_1 = new TreeColumn(tree, SWT.CENTER);
    trclmnNewColumn_1.setWidth(60);
    trclmnNewColumn_1.setText(Messages.get().getValue("Value"));

    TreeItem treeItem = new TreeItem(tree, SWT.NONE);
    treeItem.setText("Some Value");

    TreeItem trtm1 = new TreeItem(treeItem, SWT.NONE);
    trtm1.setText("Some Value");

    TreeItem trtm2 = new TreeItem(treeItem, SWT.NONE);
    trtm2.setText("Some Value");

    treeItem.setExpanded(true);

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