表格末尾显示额外的空白列

发布于 2024-12-10 14:59:21 字数 80 浏览 0 评论 0原文

我正在使用 jface tableViewer。当表中没有数据时,它会正确显示所有列,但是当数据添加到表中时,它会在表末尾显示额外的空白空间或列。

I am using jface tableViewer.When table has no data in it ,it shows all columns correctly But when Data gets added to the table it shows extra blank space or column at the end of the table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

冰之心 2024-12-17 14:59:21

我正在使用 TreeViewer + TreeViewerColumn 并且也遇到了这个问题,此解决方法也可能适用于您的 TableViewer:以编程方式设置父级调整大小时最后一列的大小:

    treeViewer.getTree().addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent e) {
            packAndFillLastColumn();
        }
    });

其中操作

// 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);
    }
}

对我来说效果很好 - 您可能想要设置列可调整大小为假;-)。当最后一列中的数据发生变化(引入/删除垂直滚动条)时也可以调用此函数。

I am using TreeViewer + TreeViewerColumn and had this problem too, this workaround might work for your TableViewer too: Programmatically set the size of the last column on parent resize:

    treeViewer.getTree().addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent e) {
            packAndFillLastColumn();
        }
    });

where the action is in

// 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);
    }
}

Works well for me - you might want to set column resizable to false ;-). This can also be called when data in the last column changes (introducting / removing vertical scroll bar).

笑饮青盏花 2024-12-17 14:59:21

谢谢托马斯。尽管我使用的是 TableViewer 和 TableColumn,但你的想法也对我有用。

引用我的代码以便其他人可以得到一些提示。

`public void controlResized(ControlEvent e) {

    if ( listOfTableColumns.size() != colProportions.length )
    {
        logger.warn( "Number of columns passed and size of column proportions array are different. " +
                "Columns resizing shall not be effective on GUI window resizing" );
        return;
    }
    Rectangle area = tableBaseComposite.getClientArea();
    Point size = theTable.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    ScrollBar vBar = theTable.getVerticalBar();
    int width = area.width - theTable.computeTrim(0,0,0,0).width - vBar.getSize().x;
    if (size.y > area.height + theTable.getHeaderHeight()) {
        // Subtract the scrollbar width from the total column width
        // if a vertical scrollbar will be required
        Point vBarSize = vBar.getSize();
        width -= vBarSize.x;
    }
    Point oldSize = theTable.getSize();

    if (oldSize.x > area.width) {
        // table is getting smaller so make the columns 
        // smaller first and then resize the table to
        // match the client area width
        int index = 0 ;
        for ( Iterator<TableColumn> iterator = listOfTableColumns.iterator(); iterator.hasNext(); )
        {
            TableColumn column = iterator.next();
            column.setWidth( (int) numberFromPercentage( width, colProportions[index++] ) );
        }
        listOfTableColumns.get( listOfTableColumns.size() - 1).pack();
        theTable.setSize(area.width, area.height);
    } else {
        // table is getting bigger so make the table 
        // bigger first and then make the columns wider
        // to match the client area width
        int index = 0;
        theTable.setSize(area.width, area.height);
        for ( Iterator<TableColumn> iterator = listOfTableColumns.iterator(); iterator.hasNext(); )
        {
            TableColumn column = iterator.next();
            column.setWidth( (int) numberFromPercentage( width, colProportions[index++] ) );
        }
        listOfTableColumns.get( listOfTableColumns.size() - 1).pack();
    }
}`

Thanks Thomas. Your idea worked for me as well, though I was using TableViewer and TableColumn.

Quoting my code so that others can take some hints.

`public void controlResized(ControlEvent e) {

    if ( listOfTableColumns.size() != colProportions.length )
    {
        logger.warn( "Number of columns passed and size of column proportions array are different. " +
                "Columns resizing shall not be effective on GUI window resizing" );
        return;
    }
    Rectangle area = tableBaseComposite.getClientArea();
    Point size = theTable.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    ScrollBar vBar = theTable.getVerticalBar();
    int width = area.width - theTable.computeTrim(0,0,0,0).width - vBar.getSize().x;
    if (size.y > area.height + theTable.getHeaderHeight()) {
        // Subtract the scrollbar width from the total column width
        // if a vertical scrollbar will be required
        Point vBarSize = vBar.getSize();
        width -= vBarSize.x;
    }
    Point oldSize = theTable.getSize();

    if (oldSize.x > area.width) {
        // table is getting smaller so make the columns 
        // smaller first and then resize the table to
        // match the client area width
        int index = 0 ;
        for ( Iterator<TableColumn> iterator = listOfTableColumns.iterator(); iterator.hasNext(); )
        {
            TableColumn column = iterator.next();
            column.setWidth( (int) numberFromPercentage( width, colProportions[index++] ) );
        }
        listOfTableColumns.get( listOfTableColumns.size() - 1).pack();
        theTable.setSize(area.width, area.height);
    } else {
        // table is getting bigger so make the table 
        // bigger first and then make the columns wider
        // to match the client area width
        int index = 0;
        theTable.setSize(area.width, area.height);
        for ( Iterator<TableColumn> iterator = listOfTableColumns.iterator(); iterator.hasNext(); )
        {
            TableColumn column = iterator.next();
            column.setWidth( (int) numberFromPercentage( width, colProportions[index++] ) );
        }
        listOfTableColumns.get( listOfTableColumns.size() - 1).pack();
    }
}`
溇涏 2024-12-17 14:59:21

不需要复杂的技巧来删除最后多余的列空间...

只需创建一个columnLayout:

TableColumnLayout columnLayout = new TableColumnLayout();

然后将其设置为每个列:

columnLayout.setColumnData(YOUR_VIEWER_COLUMN1.getColumn(), new ColumnPixelData(200));
columnLayout.setColumnData(YOUR_VIEWER_COLUMN2.getColumn(), new ColumnWeightData(200, 100));

最后,在父组合上设置布局:

parent.setLayout(columnLayout);

完整示例:

public void createPartControl(Composite parent) {
    TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);

    TableViewerColumn keyColumn = new TableViewerColumn(viewer, SWT.LEFT);
    TableViewerColumn valueColumn = new TableViewerColumn(viewer, SWT.LEFT);

    TableColumnLayout columnLayout = new TableColumnLayout();
    columnLayout.setColumnData(keyColumn.getColumn(), new ColumnPixelData(200));
    columnLayout.setColumnData(valueColumn.getColumn(), new ColumnWeightData(200, 100));
    parent.setLayout(columnLayout);
}

No need for complicated hacks to remove the extra unwanted column space at the end...

Just create a columnLayout:

TableColumnLayout columnLayout = new TableColumnLayout();

and then set it to each of your columns:

columnLayout.setColumnData(YOUR_VIEWER_COLUMN1.getColumn(), new ColumnPixelData(200));
columnLayout.setColumnData(YOUR_VIEWER_COLUMN2.getColumn(), new ColumnWeightData(200, 100));

Finally, set the layout on your parent composite:

parent.setLayout(columnLayout);

Full sample:

public void createPartControl(Composite parent) {
    TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);

    TableViewerColumn keyColumn = new TableViewerColumn(viewer, SWT.LEFT);
    TableViewerColumn valueColumn = new TableViewerColumn(viewer, SWT.LEFT);

    TableColumnLayout columnLayout = new TableColumnLayout();
    columnLayout.setColumnData(keyColumn.getColumn(), new ColumnPixelData(200));
    columnLayout.setColumnData(valueColumn.getColumn(), new ColumnWeightData(200, 100));
    parent.setLayout(columnLayout);
}
日暮斜阳 2024-12-17 14:59:21

只是猜测:也许您的列没有调整大小以填充所有表格?
如何设置列的宽度?
考虑使用 TableColumnLayout 作为表容器。

Just guessing: maybe your columns do not get resized to fill all the table?
How do you set the widths of columns?
Consider using TableColumnLayout for the table container.

只有影子陪我不离不弃 2024-12-17 14:59:21

在 Windows 上,如果已设置的所有列的净宽度小于表格的尺寸,您总是会获得额外的列/行。因此,让您的列适合您的表格总是好的,而且还为滚动条留下了一些空间,尽管我对此不太确定,但最好指定您是否需要垂直或水平滚动条。

On windows, you will always get an extra column/row if the net width of all the columns that has been set up is less than the dimension of the table. So its always good to make your columns fit your table, also there is some space left for scroll bars, though I am not very sure about this, but its always better to specify whether you want vertical or horizontal scroll bars.

青瓷清茶倾城歌 2024-12-17 14:59:21

我使用了 packAndFillLastColumn() 方法,它对我有用。但我发现了一个问题。我的表格是用边框创建的。使用 packAndFillLastColumn() 方法后,行的边框不再存在。我在 packAndFillLastColumn() 方法中使用了 setLinesVisible(true) 方法,但仍然不起作用。

I used the packAndFillLastColumn() method and it worked for me. But I found one issue with it. My table was created with a border. After using the packAndFillLastColumn() method the border for the row no longer exists. I used the setLinesVisible(true) method within the packAndFillLastColumn() method but still that does not work.

冷夜 2024-12-17 14:59:21

就这么简单!只需在 createContents 函数内的表命令中删除这一行:

table.getColumn(i).pack();

祝你好运

So simple! Just remove this line in your table commands inside the createContents function:

table.getColumn(i).pack();

Good-luck

一指流沙 2024-12-17 14:59:21

作为解决方法,使用:

- 对于 Column,

使用 TableColumnLayout 作为 treeViewer 的组合,并使用以下方法为每列设置适当的列数据:
“tableColumnLayout.setColumnData(列,new ColumnWeightData(...根据您的要求));”

-对于行,

将 GridData 设置为树查看器的组合并使用提供高度提示
“gridData.heightHint = table.getItemHeight()*numberOfVisibleRows”

As a workaround use :

-For Column

use TableColumnLayout for the treeViewer's composite and set appropriate column data for each column using:
"tableColumnLayout.setColumnData(column,new ColumnWeightData(...as per your requirement));"

-For Row

Set GridData to the treeViewer's composite and provide height hint using
"gridData.heightHint = table.getItemHeight()*numberOfVisibleRows"

假扮的天使 2024-12-17 14:59:21

我发现 Eclipse 已将其标记为 WONTFIX.. 所以无法做太多事情来删除这个空间..我们已经使用它了...:)

I found eclipse has marked it as WONTFIX.. so can not do much to remove this space..We have tp live with it...:)

梦太阳 2024-12-17 14:59:21

对于最后一列,我们需要将 setWidth 设置为窗口大小或 shell-size、parent-shell 大小,例如 1500,5000

final TableViewerColumn viewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText(title);
column.setResizable(true);
column.setMoveable(true);
//set the setWidth size upto shell size or set upto to some size like 1000,1500,2000,5000
col.setWidth(comp.getShell().getSize().x); // or col.setWidth(1500) ;
return viewerColumn;

To the end column we need to set the setWidth to window size or shell-size, parent-shell size like 1500,5000

final TableViewerColumn viewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText(title);
column.setResizable(true);
column.setMoveable(true);
//set the setWidth size upto shell size or set upto to some size like 1000,1500,2000,5000
col.setWidth(comp.getShell().getSize().x); // or col.setWidth(1500) ;
return viewerColumn;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文