裏面我有一個TableViewer
一個GridLayout
內。一切正常,表格查看器顯示爲白色區域,其中包含一些數據。 問題是,白色區域非常高,但幾乎沒有寬度。我沒有找到任何可能性來設置此查看器的大小。如何調整TableViewer的大小的網格佈局
我是否需要以某種方式調整佈局?我找不到如何!該TableViewer
似乎沒有任何填補其細胞...
什麼想法?
裏面我有一個TableViewer
一個GridLayout
內。一切正常,表格查看器顯示爲白色區域,其中包含一些數據。 問題是,白色區域非常高,但幾乎沒有寬度。我沒有找到任何可能性來設置此查看器的大小。如何調整TableViewer的大小的網格佈局
我是否需要以某種方式調整佈局?我找不到如何!該TableViewer
似乎沒有任何填補其細胞...
什麼想法?
我卷繞使用類來自動調節基於所述數據的TableViewer的寬度。
下面是我如何使用類:
activeViewer = new TableViewer(parent,
SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
final Table activeTable = activeViewer.getTable();
AutoResizeTableLayout layout = new AutoResizeTableLayout(activeTable);
activeTable.setLayout(layout);
layout.addColumnData(new ColumnWeightData(400));
layout.addColumnData(new ColumnPixelData(70));
這裏的類:
public class AutoResizeTableLayout extends TableLayout implements
ControlListener {
private final Table table;
private List<ColumnLayoutData> columns =
new ArrayList<ColumnLayoutData>();
private boolean autosizing = false;
public AutoResizeTableLayout(Table table) {
this.table = table;
table.addControlListener(this);
}
public void addColumnData(ColumnLayoutData data) {
columns.add(data);
super.addColumnData(data);
}
public void controlMoved(ControlEvent e) {
}
public void controlResized(ControlEvent e) {
if (autosizing)
return;
autosizing = true;
try {
autoSizeColumns();
} finally {
autosizing = false;
}
}
private void autoSizeColumns() {
int width = table.getClientArea().width;
// Layout is being called with an invalid value
// the first time it is being called on Linux.
// This method resets the layout to null,
// so we run it only when the value is OK.
if (width <= 1)
return;
TableColumn[] tableColumns = table.getColumns();
int size = Math.min(columns.size(), tableColumns.length);
int[] widths = new int[size];
int fixedWidth = 0;
int numberOfWeightColumns = 0;
int totalWeight = 0;
// First calc space occupied by fixed columns.
for (int i = 0; i < size; i++) {
ColumnLayoutData col = columns.get(i);
if (col instanceof ColumnPixelData) {
int pixels = ((ColumnPixelData) col).width;
widths[i] = pixels;
fixedWidth += pixels;
} else if (col instanceof ColumnWeightData) {
ColumnWeightData cw = (ColumnWeightData) col;
numberOfWeightColumns++;
int weight = cw.weight;
totalWeight += weight;
} else {
throw new IllegalStateException
("Unknown column layout data");
}
}
// Do we have columns that have a weight?
if (numberOfWeightColumns > 0) {
// Now, distribute the rest to the columns with weight.
// Make sure there's enough room, even if we have to scroll.
if (width < fixedWidth + totalWeight)
width = fixedWidth + totalWeight;
int rest = width - fixedWidth;
int totalDistributed = 0;
for (int i = 0; i < size; i++) {
ColumnLayoutData col = columns.get(i);
if (col instanceof ColumnWeightData) {
ColumnWeightData cw = (ColumnWeightData) col;
int weight = cw.weight;
int pixels = totalWeight == 0 ? 0 : weight * rest
/totalWeight;
if (pixels < cw.minimumWidth)
pixels = cw.minimumWidth;
totalDistributed += pixels;
widths[i] = pixels;
}
}
// Distribute any remaining pixels
// to columns with weight.
int diff = rest - totalDistributed;
for (int i = 0; diff > 0; i++) {
if (i == size)
i = 0;
ColumnLayoutData col = columns.get(i);
if (col instanceof ColumnWeightData) {
++widths[i];
--diff;
}
}
}
for (int i = 0; i < size; i++) {
if (tableColumns[i].getWidth() != widths[i])
tableColumns[i].setWidth(widths[i]);
}
}
}
只是作爲一個例子,如果你有一個使用佈局的容器內的表,你可以使用佈局數據設置大小:
final GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
layoutData.minimumWidth = 300;
我更喜歡TableColumnLayout或TreeColumnLayout。訣竅是將其設置爲父級組合的佈局,而不是表或樹本身。
調整父容器大小時,列大小將自動更新。
TreeColumnLayout layout = new TreeColumnLayout();
client.setLayout(layout);
viewer = new CheckboxTreeViewer(client, SWT.V_SCROLL | SWT.H_SCROLL);
viewer.getTree().setLinesVisible(true);
TreeViewerColumn firstColumn = new TreeViewerColumn(viewer, SWT.NONE);
layout.setColumnData(firstColumn.getColumn(), new ColumnWeightData(100, 10, true));
TreeViewerColumn secondColumn = new TreeViewerColumn(resourcesViewer, SWT.NONE);
layout.setColumnData(secondColumn.getColumn(), new ColumnPixelData(20, false, true));
哇這只是調整大小相當複雜,是不是有一個簡單的解決方案,像設置一個行的大小到一定的數量? – kadrian
@ ka2011r:我想你可以使用TableLayout和addColumnData方法。 –