【问题标题】:Layout problems with customized FieldEditor (Eclipse Preference Page)自定义字段编辑器的布局问题(Eclipse 首选项页面)
【发布时间】:2019-06-25 13:16:49
【问题描述】:

我尝试创建自己的 FieldEditor(因为我必须动态填充组合框值)。所以我的课扩展了'FieldEditor'。我的首选项页面需要 3 个这样的字段(第二、第三和第四字段编辑器;“选择内核”)。

显然布局出了点问题。所有字段都应该看起来像第三个字段 - 使用完整的空间。

@Override
protected void adjustForNumColumns(int numColumns) {
    ((GridData) c_top.getLayoutData()).horizontalSpan = numColumns;
}

@Override
protected void doFillIntoGrid(Composite parent, int numColumns) {

    /* Layout comments:
     * 
     * component are sequentially filled into numColumns
     * by default each component will use 1 column
     * GridData can be set to use more that one columns
     */

    GridData gd = new GridData(SWT.FILL, SWT.TOP, true, false);
    gd.horizontalSpan = numColumns;

    c_top = parent;
    c_top.setLayoutData(gd);

    c_group = new Composite(c_top, SWT.BORDER);

    GridLayout newgd = new GridLayout(2, false);
    c_group.setLayout(newgd);
    c_group.setLayoutData(gd);

    // kernel spec combo

    Label comboLabel = new Label(c_group, SWT.NONE);
    comboLabel.setText("Select kernel");
    gd = new GridData(SWT.LEFT, SWT.TOP, false, false);
    gd.horizontalSpan = numColumns - 1;
    comboLabel.setLayoutData(gd);

    c_kernelCombo = new Combo(c_group, SWT.READ_ONLY);
    gd = new GridData(SWT.FILL, SWT.TOP, true, false);
    //gd.horizontalSpan = 1;
    c_kernelCombo.setLayoutData(gd);     
}

我什至在不使用组的情况下尝试了一个更简单的布局,但我所有的字段编辑器只使用了网格的 2 个单元格(其他字段编辑器给出的 3 列看起来有点滑稽。

我不知道如何解决它。有人可以帮忙吗?

【问题讨论】:

    标签: eclipse grid-layout preferences


    【解决方案1】:

    您正在两个控件上设置GridData (gd) 的相同实例 - 这是不允许。您必须为每个控件创建一个新的GridData

    在任何情况下,您都不应该在 parent Composite 上设置布局数据 - 这不属于您的代码。

    【讨论】:

    • 遵循您的建议并没有解决问题。我找到了一个解决方案(虽然我不知道它是否是正确的解决方案)。我将作为单独的答案进行解释,我对更多的 cmets 感到高兴。
    【解决方案2】:

    我有一个解决方案,但我不知道它是否正确:

    1) 我错误地使用相同的父级创建该页面的所有 FieldEditor,尽管文档说要使用 getFieldEditorParent() 为每个字段编辑器检索一个新的父级

    2) 我猜,我误解了adjustForNumColumns(int numColumns) 的意思。我认为,它应该适应受列数变化影响的控件的水平跨度。现在我的代码如下所示:

    @Override
    protected void adjustForNumColumns(int numColumns) {
        ((GridData) c_kernelCombo.getLayoutData()).horizontalSpan = numColumns-1;
    }
    
    @Override
    protected void doFillIntoGrid(Composite parent, int numColumns) {
    
        // kernel spec combo
        Label comboLabel = new Label(parent, SWT.NONE);
        comboLabel.setText("Select kernel");
        GridData gd = new GridData(SWT.LEFT, SWT.TOP, false, false);
        gd.horizontalSpan = 1;
        comboLabel.setLayoutData(gd);
    
        c_kernelCombo = new Combo(parent, SWT.READ_ONLY);
        gd = new GridData(SWT.FILL, SWT.TOP, true, false);
        gd.horizontalSpan = numColumns - 1;
        c_kernelCombo.setLayoutData(gd);     
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多