I’m trying to display an SWT table inside a form section, but am having problems with column resizing and scrolling.
I’ve attached some sample code that can be placed in a ViewPart of an Eclipse pluging (3.7 is the version I’m using). If you run the code, and resize the column to the right, the table gains a horizontal scrollbar (which is what I want).
Now if I collapse the section (click the arrow to the left of “Section Title”), the view gains a horizontal scrollbar (which i don’t want). Expanding the section again, the view scroll remains, and now the table no longer has one.
I’m looking for a way (probably some awful combination of nested composites) to stop the table being wider than the view, and would appreciate any suggestions.
public class ExampleView extends ViewPart {
/** Note: other fields / methods removed to condense snippet */
private FormToolkit toolkit;
private ScrolledForm scrolledForm;
public void createPartControl(Composite parent) {
parent.setLayout(new FillLayout());
toolkit = new FormToolkit(parent.getDisplay());
scrolledForm = toolkit.createScrolledForm(parent);
scrolledForm.setExpandHorizontal(true);
scrolledForm.setText("Form Title");
scrolledForm.getBody().setLayout(new FillLayout());
// here's the modification for the solution
scrolledForm.getBody().setLayout(
new BoundedLayout(new FillLayout(), true));
final Section section = toolkit.createSection(scrolledForm.getBody(),
Section.DESCRIPTION | Section.TITLE_BAR | Section.TWISTIE
| Section.EXPANDED);
section.addExpansionListener(new ExpansionAdapter() {
public void expansionStateChanged(ExpansionEvent e) {
scrolledForm.reflow(true);
}
});
section.setText("Section Title");
final Table table = toolkit.createTable(section, SWT.FULL_SELECTION);
TableViewer viewer = new TableViewer(table);
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setItemCount(10);
TableColumn col = new TableColumn(table, SWT.NONE);
col.setText("Column A");
col.setWidth(150);
col.setResizable(true);
col.setMoveable(true);
TableColumn col2 = new TableColumn(table, SWT.NONE);
col2.setText("Column B");
col2.setWidth(150);
col2.setResizable(true);
col2.setMoveable(true);
section.setClient(table);
}
}
So here’s a solution i’ve come up with using a custom Layout. It wraps another layout, but can bound the
computeSizeto either the width or height of the parent composite – basically only allowing scrolling in one direction (if there is an easier way to force theScrolledFormto do this please let me know!).There are some nasty-ish reflection hacks that need to be applied as the
computeSizeandlayoutmethods areprotected