Jface:自定义 FieldEditor 布局错误

发布于 2024-12-19 04:03:47 字数 3300 浏览 0 评论 0原文

我想为首选项页面创建一个自定义 FieldEditor。目的是重写 ScaleFieldEditor 以提供一个带有两个标签的 Scale(一个显示最小值,另一个显示最大值)。

它做了一个。它正在工作,但当我将 FileFieldeditor 添加到首选项页面时,布局不好。

如果我在自定义 ScaleFieldEditor 之后立即添加 FileFieldEditor,情况会更糟: http://imageshack.us/g/844/customscalelayoutok.png/ (抱歉,我无法在这篇文章中添加图像)。

我制作了一个包含 2 个标签和比例的组合。我使用了 GridLayout:

public class ScaleWithLabelFieldEditor extends ScaleFieldEditor {

/**
 * The maximum value label
 */
private Label maxLabel;
/**
 * The minimum value label
 */
private Label minLabel;
/**
 * A composite that contains the scale and the min & max values label
 */
private Composite controls;

public ScaleWithLabelFieldEditor(String name, String labelText,
        Composite parent) {
    super(name, labelText, parent);
}

public ScaleWithLabelFieldEditor(String name, String labelText,
        Composite parent, int min, int max, int increment, int pageIncrement) {
    super(name, labelText, parent, min, max, increment, pageIncrement);
}

@Override
protected void doFillIntoGrid(Composite parent, int numColumns) {
    Control labelControl = getLabelControl(parent);
    GridData gd = new GridData();
    labelControl.setLayoutData(gd);

    controls = getControls(parent, 2);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = numColumns -1;
    gd.grabExcessHorizontalSpace = true;
    controls.setLayoutData(gd);
    updateControls();
    parent.layout();
}

/**
 * Initialize (if not done yet) the controls composite
 * 
 * @param parent
 *            the parent composite
 * @return the controls composite that contains the scaler and the min/max
 *         labels
 */
protected Composite getControls(Composite parent, int numColumns) {
    if (controls == null) {
        controls = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(numColumns, false);
        layout.numColumns = numColumns;
        controls.setLayout(layout);

        // Initialize the min value label
        minLabel = getMinLabel(controls);
        minLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
        // Initialize the max value label
        maxLabel = getMaxLabel(controls);
        maxLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
        // Initialize the scale
        scale = getScale(controls);
        scale.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, numColumns, 1));

    }
    return controls;
}

/**
 * Nothing to do, already handle with controls composite
 */
@Override
protected void adjustForNumColumns(int numColumns) {
    // Nothing to do, already handle with controls composite
}

/**
 * Update the scale and the labels above
 */
protected void updateControls() {
    if (controls != null && !controls.isDisposed()) {
        // Update scale
        scale.setMinimum(getMinimum());
        scale.setMaximum(getMaximum());
        scale.setIncrement(getIncrement());
        scale.setPageIncrement(getPageIncrement());
        // Update labels
        maxLabel.setText(Integer.toString(getMaximum()));
        minLabel.setText(Integer.toString(getMinimum()));
    }
}

我应该怎么做才能使这个字段布局起作用?我是否错过了 GridLayout/GridData 使用中的某些内容?

I'd like to create a custom FieldEditor for a preference page. the aim is to override ScaleFieldEditor to provide a Scale with two labels over it (one to display the min value, the other to display the max).

It made one. It is working but the layout is not good when I add a FileFieldeditor to preference page.

It's even worst if I add the FileFieldEditor immediately after the custom ScaleFieldEditor:
http://imageshack.us/g/844/customscalelayoutok.png/
(Sorry, I cannot add images to this post).

I made a Composite which contains the 2 Labels and the Scale. I used a GridLayout:

public class ScaleWithLabelFieldEditor extends ScaleFieldEditor {

/**
 * The maximum value label
 */
private Label maxLabel;
/**
 * The minimum value label
 */
private Label minLabel;
/**
 * A composite that contains the scale and the min & max values label
 */
private Composite controls;

public ScaleWithLabelFieldEditor(String name, String labelText,
        Composite parent) {
    super(name, labelText, parent);
}

public ScaleWithLabelFieldEditor(String name, String labelText,
        Composite parent, int min, int max, int increment, int pageIncrement) {
    super(name, labelText, parent, min, max, increment, pageIncrement);
}

@Override
protected void doFillIntoGrid(Composite parent, int numColumns) {
    Control labelControl = getLabelControl(parent);
    GridData gd = new GridData();
    labelControl.setLayoutData(gd);

    controls = getControls(parent, 2);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = numColumns -1;
    gd.grabExcessHorizontalSpace = true;
    controls.setLayoutData(gd);
    updateControls();
    parent.layout();
}

/**
 * Initialize (if not done yet) the controls composite
 * 
 * @param parent
 *            the parent composite
 * @return the controls composite that contains the scaler and the min/max
 *         labels
 */
protected Composite getControls(Composite parent, int numColumns) {
    if (controls == null) {
        controls = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(numColumns, false);
        layout.numColumns = numColumns;
        controls.setLayout(layout);

        // Initialize the min value label
        minLabel = getMinLabel(controls);
        minLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
        // Initialize the max value label
        maxLabel = getMaxLabel(controls);
        maxLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
        // Initialize the scale
        scale = getScale(controls);
        scale.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, numColumns, 1));

    }
    return controls;
}

/**
 * Nothing to do, already handle with controls composite
 */
@Override
protected void adjustForNumColumns(int numColumns) {
    // Nothing to do, already handle with controls composite
}

/**
 * Update the scale and the labels above
 */
protected void updateControls() {
    if (controls != null && !controls.isDisposed()) {
        // Update scale
        scale.setMinimum(getMinimum());
        scale.setMaximum(getMaximum());
        scale.setIncrement(getIncrement());
        scale.setPageIncrement(getPageIncrement());
        // Update labels
        maxLabel.setText(Integer.toString(getMaximum()));
        minLabel.setText(Integer.toString(getMinimum()));
    }
}

What I should do to make this field layout work? Did I miss something in GridLayout/GridData use?

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

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

发布评论

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

评论(2

护你周全 2024-12-26 04:03:47

问题似乎是您没有实现 adjustmentForNumColumns(int numColumns) 方法。添加 FileFieldEditor 后,列数从 2 更改为 3。并且您的字段编辑器必须适应当前的情况。

The problem seems to be, that you did not implement the adjustForNumColumns(int numColumns) method. When you add the FileFieldEditor the number of columns changes from 2 to 3. And your field editor must adapt to that which it currently does not.

不羁少年 2024-12-26 04:03:47

看起来horizo​​ntalSpan直到最后才跨越?因此,您可能应该尝试删除 gd.horizo​​ntalSpan = numColumns -1; 中的 -1;

It seems like the horizontalSpan is not spanning until the end? So, may be you should try to remove -1 in gd.horizontalSpan = numColumns -1;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文