Saji Liyanage
Saji Liyanage

Reputation: 35

How to create a Java SWT table with multiple columns without giving static column width

I'm currently developing an eclipse plugin and in that plugin, there is a form view as a design template. In that form view, I have added a Table and there should have two columns in width ratio of 1:2. And also I want that table to be responsive and dynamically change its column width in order to the formView page width.

The following code segment is the one I'm currently using.

        Table table = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
        fd = new FormData();
        fd.height = 200;
        fd.top = new FormAttachment(removeTestCaseButton, 5);
        fd.left = new FormAttachment(1);
        fd.right = new FormAttachment(99);
        table.setLayoutData(fd);
        table.setLinesVisible(true);
        table.setHeaderVisible(true);

        TableColumn column1 = new TableColumn(testCaseTable, SWT.CENTER);
        column.setText("column One");

        TableColumn column2 = new TableColumn(testCaseTable, SWT.CENTER);
        column2.setText("column Two");

        form.addControlListener(new ControlAdapter() {
            public void controlResized(ControlEvent e) {
                Rectangle area = form.getBody().getClientArea();
                int width = area.width;         
                column1.setWidth(width / 3);
                column1.setWidth(width * 2 / 3);
            }
        });

But here the problem is when I open the FormView it works fine. But my table is inside a Section. Once I expand or collapse the Section the table width is getting increased with appearing the horizontal scrollbar.

I just want a solid solution for this.

Upvotes: 2

Views: 1177

Answers (1)

greg-449
greg-449

Reputation: 111142

This is much easier to do using the JFace TableViewer with the TableColumnLayout and ColumnWeightData, but you will have to rework your code to use JFace style content and label providers for the table.

TableColumnLayout tableLayout = new TableColumnLayout();

// A separate composite containing just the table viewer is required
Composite tableComp = new Composite(parent, SWT.NONE);

tableComp.setLayout(tableLayout);

TableViewer viewer = new TableViewer(tableComp, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);

TableViewerColumn col1 = new TableViewerColumn(viewer, SWT.LEAD);
col1.getColumn().setText("Column 1");

col1.setLabelProvider(.... label provider for column 1 ....);

// Weight for column
tableLayout.setColumnData(col1.getColumn(), new ColumnWeightData(60));

TableViewerColumn col2 = new TableViewerColumn(viewer, SWT.LEAD);
col2.getColumn().setText("Column 2");

col2.setLabelProvider(....... label provider for column 2 .....);

// Weight for column
tableLayout.setColumnData(col2.getColumn(), new ColumnWeightData(40));

viewer.getTable().setHeaderVisible(true);
viewer.getTable().setLinesVisible(true);

viewer.setContentProvider(ArrayContentProvider.getInstance());

viewer.setInput(.... input data for the viewer ....);

Upvotes: 3

Related Questions