【问题标题】:Resizing table with shell and aligning buttons?使用外壳和对齐按钮调整表格大小?
【发布时间】:2018-06-30 01:29:48
【问题描述】:

如果我拖动 shell 的边框,有人可以告诉我如何调整表格的大小。换句话说,我希望我的表端连接到外壳,这样如果外壳正在调整大小,表就会跟随。我也有按钮的问题。我需要将它们定位在右下角,在桌子下方,但是我为对齐设置了什么(GridData.HORIZONTAL_ALIGN_BEGINNING 或 new GridData(SWT.END, SWT.END, false, false) 或 new GridData(SWT.END, SWT.BOTTOM, false, false) 和输出一样) 它将我的两个按钮设置在左下角,一个在右下角。这是代码和表格的图片:

try {
        display = new Display();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    shell = new Shell(display);
    shell.setLayout(new GridLayout());

    GridLayout layout = new GridLayout();
    shell.setLayout(layout);

    Composite composite = new Composite(shell, SWT.NONE);
    // Create a composite to hold the children
    GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_BOTH);
    composite.setLayoutData(gridData);

    // Set numColumns to 3 for the buttons
    GridLayout layout1 = new GridLayout(3, false);
    layout1.marginWidth = 4;
    composite.setLayout(layout1);

    int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | 
            SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
    final int NUMBER_COLUMNS = 6;
    table = new Table(composite, style);

    GridData gridData1 = new GridData(GridData.FILL_BOTH);
    gridData1.grabExcessVerticalSpace = true;
    gridData1.horizontalSpan = 3;
    table.setLayoutData(gridData1);

    table.setLinesVisible(true);
    table.setHeaderVisible(true);

    String[] titles = { methodHeader, messageHeader, parametersHeader, resultHeader, deltaHeader, assertionHeader};
    for (int i = 0; i < titles.length; i++) {
        TableColumn column = new TableColumn(table, SWT.LEFT, i);
        column.setText(titles[i]);
        table.getColumn(i).pack();
    }


    addDataToTableModel(methodList);

    GridData gridData2 = new GridData(SWT.END, SWT.END, false, false);
    gridData2.widthHint = 80;

    buttonOk = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonOk.setText(ok);
    buttonOk.setLayoutData(gridData2);

    buttonCancel = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonCancel.setText(cancel);
    /*GridData gridData3 = new GridData(SWT.END, SWT.END, false, false);
    gridData3.widthHint = 80;*/
    buttonCancel.setLayoutData(gridData2);

    buttonHelp = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonHelp.setText(help);
    /*GridData gridData4 = new GridData(SWT.END, SWT.END, false, false);
    gridData4.widthHint = 80;*/
    buttonHelp.setLayoutData(gridData2); 

左侧 2 个按钮,右侧 1 个按钮。

编辑1: 尝试为每个按钮使用不同的 GridData:

    GridData gridData2 = new GridData(SWT.END, SWT.END, false, false);
    gridData2.widthHint = 80;

    buttonOk = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonOk.setText(ok);
    buttonOk.setLayoutData(gridData2);

    buttonCancel = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonCancel.setText(cancel);
    GridData gridData3 = new GridData(SWT.END, SWT.END, false, false);
    gridData3.widthHint = 80;
    buttonCancel.setLayoutData(gridData3);

    buttonHelp = new Button(composite, SWT.PUSH | SWT.CENTER);
    buttonHelp.setText(help);
    GridData gridData4 = new GridData(SWT.END, SWT.END, false, false);
    gridData4.widthHint = 80;
    buttonHelp.setLayoutData(gridData4); 

按钮的结果相同。

【问题讨论】:

  • 不要对多个控件重复使用GridData - 这不起作用。每个控件都需要一个新的GridData
  • 是的,我试过这两个,结果一样:(
  • 我没有说它会解决问题,我说这是错误的代码。
  • 好的,谢谢,我没明白 :)

标签: java eclipse button swt


【解决方案1】:

使用 4 列布局:

final GridLayout layout1 = new GridLayout(4, false);

并使用一个空的填充标签来抓住按钮左侧的空间:

Label filler = new Label(composite, SWT.LEAD);
filler.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

GridData gridData2 = new GridData(SWT.END, SWT.END, false, false);
gridData2.widthHint = 80;

Button buttonOk = new Button(composite, SWT.PUSH | SWT.CENTER);
buttonOk.setText("OK");
buttonOk.setLayoutData(gridData2);

gridData2 = new GridData(SWT.END, SWT.END, false, false);
gridData2.widthHint = 80;

Button buttonCancel = new Button(composite, SWT.PUSH | SWT.CENTER);
buttonCancel.setText("Cancel");
buttonCancel.setLayoutData(gridData2);

gridData2 = new GridData(SWT.END, SWT.END, false, false);
gridData2.widthHint = 80;

Button buttonHelp = new Button(composite, SWT.PUSH | SWT.CENTER);
buttonHelp.setText("Help");
buttonHelp.setLayoutData(gridData2);

【讨论】:

  • 谢谢这解决了我的按钮问题,现在我又留下了一个,如果调整外壳大小,如何删除最后一列(空格)并调整表格大小?
  • 在调整外壳大小时表格确实会调整大小 - 你的意思是调整列的大小吗?我不知道你会如何只使用 Table,因为我总是使用 JFace TableViewer。
  • 是的,这就是我的意思,我之前使用了 swing,并且使用框架调整了列的大小。另外,如果我使用 TableViewer,我可以编辑单元格吗?
  • 好的,所以你能给我一些链接来编辑我表格中的这些空单元格,并在单击“确定”按钮后逐行获取它的内容,因为我找不到任何类似的例子。
猜你喜欢
  • 2012-01-28
  • 1970-01-01
  • 2012-04-23
  • 2012-03-02
  • 2017-01-29
  • 1970-01-01
  • 2021-09-20
  • 2016-01-21
  • 2013-05-20
相关资源
最近更新 更多