7

FieldEditorPreferencePage 中的布局设置存在以下问题。
我的代码是这样的:

public void createFieldEditors () {
  Group pv = new group(getfieldEditorParent(), SWT.SHADOW_OUT);
  Group of = new group(getfieldEditorParent(), SWT.SHADOW_OUT);
  pv.setText(“pv”);
  of.setText(“of”);
  GridLayout layout = new GridLayout(2,false);
  pv.setLayout(layout);
  of.setLayout(layout);
  addField(new StringFieldEditor(“PreferenceStore name”,“Text:”, pv);
  addField(new StringFieldEditor(“PreferenceStore name”,“Text:”, pv);
  addField(new StringFieldEditor(“PreferenceStore name”,“Text:”, of);
  addField(new StringFieldEditor(“PreferenceStore name”,“Text:”, of);
  and so on.
 }

问题是它不适用于 GridLayout。
StringFieldEditors 不是并行的。列数始终为 1。此外,当我尝试更改组中 StringFieldEditors 的大小时,它也不起作用。

有人有什么想法吗?
谢谢。

4

5 回答 5

7

问题是,当您使用 时FieldEditorPreferencePage,您只能使用FieldEditor子类作为组件。这是文档中的一个片段:

FieldEditorPreferencePage 实现了一个页面,该页面使用这些字段编辑器在页面上显示和存储首选项值。FieldEditorPreferencePage 子类不是创建 SWT 控件来填充其内容,而是创建字段编辑器来显示内容。 页面上的所有字段都必须实现为字段编辑器

这意味着您有两种选择如何实现您想要的:

  1. 实现您自己的 子类FieldEditor,它将代表 Group 小部件。
  2. 不要扩展FieldEditorPreferencePage,而只是 a PreferencePage。然后你必须实现createContents方法而不是createFieldEditors. 您还必须管理属性的加载和保存。

如果您想提供一些复杂的布局,我认为第二种方法可能更容易。您可以在这里找到更多信息

于 2009-03-31T14:24:59.357 回答
3

另一个(简单的)解决方法:您还可以创建新的 Composites 来创建更多列。问题是这些 FieldEditor 与它们的父级通信并弄乱了您的布局。因此,通过创建一个“空”复合,他们可以随心所欲地进行交流:)

someGroup = new Group(..., SWT.NONE);
someGroup .setLayout(new GridLayout(16, false));

Composite myC1= new Composite(someGroup,SWT.NONE);
addField(new BooleanFieldEditor(...,C1);

Composite myC2= new Composite(someGroup,SWT.NONE);
addField(new BooleanFieldEditor(...,C2);
于 2013-11-20T11:04:36.880 回答
1

FieldEditorPreferencePage关于(GRID风格)需要了解的两件事:

  1. 字段编辑器父级的布局始终设置GridLayout为“自定义”组件,例如Groups;
  2. 布局中的列数根据任何字段编辑器中的最大组件数进行调整(在StringFieldEditor).

在上面的例子中,布局数据Groups应该考虑到这一点:

GridDataFactory.defaultsFor(pv).grab(true, false).span(2, 1).applyTo(pv);
GridDataFactory.defaultsFor(of).grab(true, false).span(2, 1).applyTo(of);
于 2011-11-14T07:25:17.537 回答
1

我实现了 Group-FieldEditor,它可以包含其他 FieldEditor 并将它们布置为一个组。


import java.util.Collection;

import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

/**
 * Class is intended to create a Group Widgets, inside of the {@link FieldEditorPreferencePage}
 * objects.
 * This class should be used as following:
 * 
    *
  • use the {@link #getFieldEditorParent()} to as a parent, while creating new Field Editors. *
  • use {@link #setFieldEditors(Collection)} to add the collection of FieldEditors to the * {@link GroupFieldEditor}. *
* * @author alf * */ public class GroupFieldEditor extends FieldEditor { private String name; private Collection members; private int numcolumns; private Group group; private Composite parent; /** * The gap outside, between the group-frame and the widgets around the group */ private static final int GROUP_PADDING = 5; // px /** * The gap inside, between the group-frame and the content */ private static final int GROUP_VERTICAL_MARGIN = 5; // px /** * The inside-distance creates a new boolean field editor */ protected GroupFieldEditor() { } /** * Creates a Group of {@link FieldEditor} objects * * @param name * - name * @param fieldEditorParent * - parent */ public GroupFieldEditor(String name, Composite fieldEditorParent) { this.name = name; // the parent is a Composite, which is contained inside of the preference page. Initially it // does not have any layout. this.parent = fieldEditorParent; FillLayout fillLayout = new FillLayout(); fillLayout.marginHeight = GROUP_VERTICAL_MARGIN; this.parent.setLayout(fillLayout); this.group = new Group(parent, SWT.DEFAULT); this.group.setText(this.name); } /** * The parent for all the FieldEditors inside of this Group. * * @return - the parent */ public Composite getFieldEditorParent() { return group; } /** * Sets the FieldeditorChildren for this {@link GroupFieldEditor} * * @param membersParam */ public void setFieldEditors(Collection membersParam) { this.members = membersParam; doFillIntoGrid(getFieldEditorParent(), numcolumns); } /* * (non-Javadoc) Method declared on FieldEditor. */ @Override protected void adjustForNumColumns(int numColumns) { this.numcolumns = numColumns; } /* * (non-Javadoc) Method declared on FieldEditor. */ @Override protected void doFillIntoGrid(Composite parentParam, int numColumns) { GridLayout gridLayout = new GridLayout(); gridLayout.marginLeft = GROUP_PADDING; gridLayout.marginRight = GROUP_PADDING; gridLayout.marginTop = GROUP_PADDING; gridLayout.marginBottom = GROUP_PADDING; this.group.setLayout(gridLayout); this.parent.layout(); this.parent.redraw(); if (members != null) { for (FieldEditor editor : members) { editor.fillIntoGrid(getFieldEditorParent(), 1); } } } /* * (non-Javadoc) Method declared on FieldEditor. Loads the value from the * preference store and sets it to the check box. */ @Override protected void doLoad() { if (members != null) { for (FieldEditor editor : members) { editor.load(); } } } /* * (non-Javadoc) Method declared on FieldEditor. Loads the default value * from the preference store and sets it to the check box. */ @Override protected void doLoadDefault() { if (members != null) { for (FieldEditor editor : members) { editor.loadDefault(); } } } /* * (non-Javadoc) Method declared on FieldEditor. */ @Override protected void doStore() { if (members != null) { for (FieldEditor editor : members) { editor.store(); } } } @Override public void store() { super.store(); doStore(); } /* * (non-Javadoc) Method declared on FieldEditor. */ @Override public int getNumberOfControls() { return 1; } /* * (non-Javadoc) Method declared on FieldEditor. */ @Override public void setFocus() { if (members != null && !members.isEmpty()) { members.iterator().next().setFocus(); } } /* * @see FieldEditor.setEnabled */ @Override public void setEnabled(boolean enabled, Composite parentParam) { if (members != null) { for (FieldEditor editor : members) { editor.setEnabled(enabled, parentParam); } } } @Override public void setPreferenceStore(IPreferenceStore store) { super.setPreferenceStore(store); if (members != null) { for (FieldEditor editor : members) { editor.setPreferenceStore(store); } } } }
于 2012-10-17T07:36:39.010 回答
0

另一种解决方法:

使用标签来分隔字段组。下面创建一个垂直行分隔符并将文本直接放在其下方:

new Label(getFieldEditorParent(), SWT.SEPARATOR | SWT.HORIZONTAL)
        .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
new Label(getFieldEditorParent(), SWT.NONE).setText("My Group Title");
于 2018-05-17T09:32:47.003 回答