Pra*_*ant 3 swt jface eclipse-rcp
我正在使用 jface tableViewer。当表中没有数据时,它会正确显示所有列但是当数据被添加到表中时,它会在表的末尾显示额外的空格或列。
我正在使用 TreeViewer + TreeViewerColumn 并且也有这个问题,这个解决方法也可能适用于您的 TableViewer:以编程方式设置父调整大小的最后一列的大小:
treeViewer.getTree().addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
packAndFillLastColumn();
}
});
Run Code Online (Sandbox Code Playgroud)
动作在哪里
// Resize last column in tree viewer so that it fills the client area completely if extra space.
protected void packAndFillLastColumn() {
Tree tree = treeViewer.getTree();
int columnsWidth = 0;
for (int i = 0; i < tree.getColumnCount() - 1; i++) {
columnsWidth += tree.getColumn(i).getWidth();
}
TreeColumn lastColumn = tree.getColumn(tree.getColumnCount() - 1);
lastColumn.pack();
Rectangle area = tree.getClientArea();
Point preferredSize = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT);
int width = area.width - 2*tree.getBorderWidth();
if (preferredSize.y > area.height + tree.getHeaderHeight()) {
// Subtract the scrollbar width from the total column width
// if a vertical scrollbar will be required
Point vBarSize = tree.getVerticalBar().getSize();
width -= vBarSize.x;
}
// last column is packed, so that is the minimum. If more space is available, add it.
if(lastColumn.getWidth() < width - columnsWidth) {
lastColumn.setWidth(width - columnsWidth);
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说效果很好 - 您可能想将可调整大小的列设置为 false ;-)。这也可以在最后一列中的数据更改时调用(引入/删除垂直滚动条)。