我想为首选项页面创建一个自定义的FieldEditor。其目的是覆盖ScaleFieldEditor以提供一个带有两个标签的比例(一个用于显示最小值,另一个用于显示最大值)。
它制造了一个。它可以工作,但布局不是很好,当我添加一个FileFieldeditor到首选项页面。
如果我在自定义的ScaleFieldEditor:http://imageshack.us/g/844/customscalelayoutok.png/后面立即添加FileFieldEditor,那就更糟了(对不起,我不能在这篇文章中添加图片)。
我做了一个包含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的使用中遗漏了什么?
发布于 2011-12-02 16:39:05
问题似乎是,您没有实现adjustForNumColumns(int numColumns)方法。添加FileFieldEditor时,列数将从2更改为3。字段编辑器必须适应当前不能适应的列数。
发布于 2011-12-02 00:07:39
看起来horizontalSpan是不是直到最后才会跨越?因此,您可能应该尝试删除gd.horizontalSpan = numColumns -1中的-1;
https://stackoverflow.com/questions/8343000
复制相似问题