我一直負責創建包含兩個TableViewer的Composite。頂部表格包含一行可編輯單元格。這些表格單元格將用於過濾底部表格中的數據。有很多事情不能正常工作,但我將這裏的調查限制在一個特定的範圍內。 TableViewers的表格超出了包含它們的Composite的寬度和高度,並且我無法查看複合材料右邊緣以外的表格列(或底部表格下方的行)。複合材料的水平滾動條正確滾動,但只有複合材料的寬度(毫不奇怪)。我確信這與在構造過程中調整GridData或SWT常量一樣簡單。可悲的是,我嘗試了幾種不同的組合,但都沒有成功。這裏是我的相關代碼片段:試圖創建包含兩個TableViewer的org.eclipse.swt.widgets.Composite的問題
public class ControlledColumnFilterTableComposite<T> extends Composite {
private TableViewer filterViewer;
private TableViewer mainViewer;
public ControlledColumnFilterTableComposite(Composite parent,
IControlledColumnTableContentProvider<T> content) {
super(parent, SWT.H_SCROLL);
createComposite(parent, content);
}
private void createComposite(Composite parent,
IControlledColumnTableContentProvider<T> content) {
this.setLayout(new GridLayout(1, false));
GridData data = getLayoutData(parent);
this.setLayoutData(data);
addHorizontalBarListener();
this.filterViewer = createFilterViewer(parent, content);
this.mainViewer = createMainViewer(parent, content);
}
private TableViewer createFilterViewer(Composite parent,
IControlledColumnTableContentProvider<T> content) {
TableViewer viewer = new TableViewer(this, SWT.MULTI
| SWT.FULL_SELECTION | SWT.BORDER);
viewer.getTable().setHeaderVisible(true);
viewer.getTable().setLinesVisible(true);
viewer.setContentProvider(content);
ControlledColumnTableMetadata<T> metadata = content.getMetadata();
for (int i = 0; i < metadata.getBean().getDeclaredFields().length; i++) {
getViewerColumn(viewer, metadata.getBean().getDeclaredFields()[i],
true);
}
return viewer;
}
private TableViewer createMainViewer(Composite parent,
IControlledColumnTableContentProvider<T> content) {
TableViewer viewer = new TableViewer(this, SWT.MULTI | SWT.V_SCROLL
| SWT.FULL_SELECTION | SWT.BORDER);
viewer.getTable().setHeaderVisible(false);
viewer.getTable().setLinesVisible(true);
viewer.setContentProvider(content);
viewer.setInput(((IPagingContentProvider<T>) content)
.getModelProvider().getTableRows());
ControlledColumnTableMetadata<T> metadata = content.getMetadata();
for (int i = 0; i < metadata.getBean().getDeclaredFields().length; i++) {
getViewerColumn(viewer, metadata.getBean().getDeclaredFields()[i],
false);
}
viewer.getTable().setLayoutData(getLayoutData(parent));
/*
* I've tried getLayoutData(this) and I've also tried manipulating the
* grabExcessHorizontalSpace and grabExcessVerticalSpace on the returned
* GridData without getting the desired results
*/
return viewer;
}
private TableViewerColumn getViewerColumn(TableViewer viewer, Field field,
boolean listen) {
final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
setColumnProperties(viewer, column, field);
viewerColumn.setLabelProvider(getColumnLabelProvider(field));
// Only the filter TableViewer should have Listeners and EditingSupport
if (listen) {
// add sorting support
viewerColumn.getColumn().addListener(SWT.Selection,
new SortListener(field));
// add support for saving column width preferences
viewerColumn.getColumn()
.addControlListener(createControlListener());
// add editing support
viewerColumn.setEditingSupport(getEditingSupport(viewer, field));
}
return viewerColumn;
}
private void setColumnProperties(TableViewer viewer, TableColumn column,
Field field) {
ControlledColumnTableMetadata<T> metadata = ((IControlledColumnTableContentProvider<T>) viewer
.getContentProvider()).getMetadata();
column.setText(metadata.getColumnHeader(field));
column.setToolTipText(metadata.getColumnHeader(field));
if (metadata.isVisible(field)) {
column.setWidth(metadata.getColumnWidth(field));
column.setResizable(true);
column.setMoveable(true);
} else {
column.setWidth(0);
column.setResizable(false);
column.setMoveable(false);
}
}
private GridData getLayoutData(Composite parent) {
GridLayout layout = (GridLayout) parent.getLayout();
// Layout the viewer
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalAlignment = GridData.FILL;
gridData.horizontalSpan = layout.numColumns;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
return gridData;
}
...
}
任何建議非常感謝。提前致謝。
謝謝Tomasz。可悲的是,這個建議並沒有真正奏效。使用此解決方案,頂部表格消失,底部表格出現垂直和水平滾動條。我實際上需要垂直滾動條,但我需要兩個表水平滾動,所以我需要抑制底部表的水平滾動條,並允許Composite的水平滾動條進行水平滾動。它仍然沒有擴展表格列的長度。我嘗試使用ScrolledComposite,但得到了更加奇怪的結果。所以我只寫了自己的滾動代碼。 – 2011-01-31 20:27:28