【问题标题】:How to adjust the table height to the number of items in SWT with GridLayout?如何使用 GridLayout 将表格高度调整为 SWT 中的项目数?
【发布时间】:2016-12-18 06:52:08
【问题描述】:

我想要一张高度根据项目数量精确调整的表格。我正在使用 SWT,我正在使用 GridLayout。这是我的代码的摘录:

Table table = new Table(parent, SWT.BORDER);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn tc1 = new TableColumn(table, SWT.CENTER);
TableColumn tc2 = new TableColumn(table, SWT.CENTER);
tc1.setText("Column1");
tc2.setText("Column2");
tc1.setWidth(85);
tc2.setWidth(90);

for (int i = 0; i < 3; i++) {
    TableItem item = new TableItem(table, SWT.NONE);
    item.setText(0, "Text Column1");
    item.setText(1, "Text Column2");
}

GridData gridData = new GridData(tc1.getWidth()+tc2.getWidth()-17, ???);
table.setLayoutData(gridData);

对于宽度,我找到了一个公式。但是身高呢?

【问题讨论】:

    标签: java swt grid-layout


    【解决方案1】:

    使用Table::getItemHeight() 计算给定物品数量所需的客户高度。然后使用computeTrim() 计算给定客户端高度的界限。如果显示网格线,则添加每条分隔线的宽度。最后,您可以使用GridDataheightHint 来建议GridLayout 为桌子预留一定的高度。

    例如:

    int clientWidth = ...
    int clientHeight = table.getItemHeight * numberOfItems;
    if( table.getLinesVisible() && numberOfItems > 0 ) {
      clientHeight += table.getGridLineWidth() * ( numberOfItems - 1 );
    }
    Rectangle bounds = table.computeTrim( 0, 0, clientWidth, clientHeight );
    
    GridData gridData = new GridData( SWT.DEFAULT, clientHeight );
    

    如果您的客户端宽度是在其他地方计算的,您也可以将其设置为零。

    【讨论】:

    • 谢谢! int clientHeight = (table.getItemHeight() * items.length)+7;为我工作
    • 当使用像 '7' 这样的恒定像素偏移时,请记住,这些不适用于所有平台/屏幕分辨率/环境设置。
    • 感谢您的提示!
    猜你喜欢
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 2021-10-14
    • 1970-01-01
    • 2014-10-01
    • 2021-07-19
    • 2010-12-25
    相关资源
    最近更新 更多