java - 调整表格列的大小以填充所有可用空间

标签 java eclipse swt

我在 Eclipse SWT 中有这个表,有 5 列。我调整了窗口和整个表格的大小,但列的大小没有调整以填充所有可用空间。

是否可以使用一种布局方法来使列自行调整大小以填充所有可用空间?我发现了一些代码可以在调整客户空间大小时调整列的大小,但这对我来说似乎是一个小技巧。

肯定有一种优雅的方式可以通过使用布局本身来做到这一点。

最佳答案

这可能会派上用场:

Composite tableComposite = new Composite(parent, SWT.NONE);
TableViewer xslTable = new TableViewer(tableComposite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
xslTable.getTable().setLinesVisible(true);
xslTable.getTable().setHeaderVisible(true);
TableViewerColumn stylesheetColumn = new TableViewerColumn(xslTable, SWT.NONE);
stylesheetColumn.getColumn().setText(COLUMN_NAMES[0]);
stylesheetColumn.getColumn().setResizable(false);
TableViewerColumn conceptColumn = new TableViewerColumn(xslTable, SWT.NONE);
conceptColumn.getColumn().setText(COLUMN_NAMES[1]);
conceptColumn.getColumn().setResizable(false);
TableColumnLayout tableLayout = new TableColumnLayout();
tableComposite.setLayout(tableLayout);

layoutTableColumns();

layoutTableColumns 方法

  /**
   * Resize table columns so the concept column is packed and the stylesheet column takes the rest of the space
   */
  private void layoutTableColumns()
  {
    // Resize the columns to fit the contents
    conceptColumn.getColumn().pack();
    stylesheetColumn.getColumn().pack();
    // Use the packed widths as the minimum widths
    int stylesheetWidth = stylesheetColumn.getColumn().getWidth();
    int conceptWidth = conceptColumn.getColumn().getWidth();
    // Set stylesheet column to fill 100% and concept column to fit 0%, but with their packed widths as minimums
    tableLayout.setColumnData(stylesheetColumn.getColumn(), new ColumnWeightData(100, stylesheetWidth));
    tableLayout.setColumnData(conceptColumn.getColumn(), new ColumnWeightData(0, conceptWidth));
  }

关于java - 调整表格列的大小以填充所有可用空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9211106/

相关文章:

java - 应用程序自动在宽度和高度上增加 10 个像素

java - 在 Java 中删除 XML 元素

eclipse - 如何修复 Subversion 锁定错误

java - 模拟 SWT 打印机

java - 文本编辑器的语法突出显示

java - 如何在 SWT Java 上创建 "deselecting"监听器

java - 如何从句子中删除停用词?

java - 为什么当类存在时会抛出NoClassDefFoundError?

eclipse - 将 CGI-BIN 添加到 Eclipse 上的 Tomcat 7

java - 读写文件 - Java 和 Android