如何删除合成顶部不需要的 space?
How to remove unwanted space on top of a composite?
我正在尝试创建自定义表单编辑器页面。在表单页面内,我需要有一个 table,我可以在其中列出键值对。属性列表 table 我想创建为可重用组件。
我已经创建了表单,但是在属性 table 组合之前出现不需要的 space(见下图)。我怎样才能摆脱这个空 space?
下面是我的表单页面代码
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.editor.FormPage;
public class EnvironmentPage extends FormPage {
public EnvironmentPage(FormEditor editor) {
super(editor, Activator.PLUGIN_ID + ".amf.environment", "Environment");
}
@Override
protected void createFormContent(IManagedForm managedForm) {
FormToolkit toolkit = managedForm.getToolkit();
ScrolledForm form = managedForm.getForm();
form.setText("Environment");
Composite body = form.getBody();
GridLayout gridLayout = new GridLayout(2, true);
body.setLayout(gridLayout);
toolkit.paintBordersFor(body);
Composite leftComposite = toolkit.createComposite(body, SWT.NONE);
leftComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
GridLayout leftCompositeLayout = new GridLayout();
leftCompositeLayout.marginWidth = 0;
leftCompositeLayout.marginHeight = 0;
leftComposite.setLayout(leftCompositeLayout);
createPropertiesSection(toolkit, leftComposite);
Composite rightComposite = toolkit.createComposite(body, SWT.NONE);
rightComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
GridLayout rightCompositeLayout = new GridLayout();
rightCompositeLayout.marginWidth = 0;
rightCompositeLayout.marginHeight = 0;
rightComposite.setLayout(rightCompositeLayout);
// TODO Controls on left composite
super.createFormContent(managedForm);
}
private void createPropertiesSection(FormToolkit toolkit, Composite leftComposite) {
Section section = toolkit.createSection(leftComposite,
ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED | ExpandableComposite.TWISTIE);
section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
section.setText("Log4J Properties");
Composite log4jComposite = toolkit.createComposite(section, SWT.BORDER);
toolkit.adapt(log4jComposite);
GridLayout gridLayout = new GridLayout();
gridLayout.marginBottom = 0;
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
log4jComposite.setLayout(gridLayout);
log4jComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
section.setClient(log4jComposite);
PropertiesEditor propsEditor = new PropertiesEditor(log4jComposite, SWT.NONE) {
@Override
public void loadData() {
// TODO Auto-generated method stub
}
};
propsEditor.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
toolkit.paintBordersFor(section);
}
}
在属性编辑器下方class
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.widgets.FormToolkit;
public abstract class PropertiesEditor extends Composite {
protected FormToolkit toolkit;
protected Button btnRemove;
protected Button btnAdd;
protected Table propertiesTable;
public PropertiesEditor(Composite parent, int style) {
super(parent, style);
toolkit = new FormToolkit(parent.getDisplay());
createComposite(parent);
}
private void createComposite(Composite parent) {
Composite propertiesGroup = toolkit.createComposite(parent, SWT.BORDER);
GridLayout gridLayout = new GridLayout(2, false);
gridLayout.marginWidth = 1;
gridLayout.marginHeight = 1;
gridLayout.verticalSpacing = 1;
propertiesGroup.setLayout(gridLayout);
propertiesGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
propertiesTable = toolkit.createTable(propertiesGroup, SWT.BORDER | SWT.FULL_SELECTION);
propertiesTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
propertiesTable.setHeaderVisible(true);
propertiesTable.setLinesVisible(true);
propertiesTable.setData("name", "properties-table");
toolkit.adapt(propertiesTable, true, true);
TableColumn tableColumnKey = new TableColumn(propertiesTable, SWT.NONE);
tableColumnKey.setText("Key");
TableColumn tableColumValue = new TableColumn(propertiesTable, SWT.NONE);
tableColumValue.setText("Value");
propertiesTable.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
tableColumnKey.setWidth((int) (propertiesTable.getClientArea().width * 0.3));
tableColumValue.setWidth((int) (propertiesTable.getClientArea().width * 0.7));
}
});
Composite buttonsGroup = toolkit.createComposite(propertiesGroup, SWT.NONE);
buttonsGroup.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true, 1, 1));
GridLayout buttonsLayout = new GridLayout();
buttonsLayout.marginWidth = 10;
buttonsGroup.setLayout(buttonsLayout);
new Label(buttonsGroup, SWT.NONE);
new Label(buttonsGroup, SWT.NONE);
btnAdd = toolkit.createButton(buttonsGroup, "Add", SWT.NONE);
GridData gd_btnAdd = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
gd_btnAdd.widthHint = 60;
btnAdd.setLayoutData(gd_btnAdd);
toolkit.adapt(btnAdd, true, true);
btnRemove = toolkit.createButton(buttonsGroup, "Remove", SWT.NONE);
GridData gd_btnRemove = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
gd_btnRemove.widthHint = 60;
btnRemove.setLayoutData(gd_btnRemove);
toolkit.adapt(btnRemove, true, true);
}
public abstract void loadData();
}
你的 PropertiesEditor
扩展了 Composite
但是你没有对该组合做任何事情,而是在 createComposite
方法中创建另一个 Composite
,其父级是传递的父级到 PropertiesEditor
构造函数。我认为顶部的差距是 PropertiesEditor
复合。
我正在尝试创建自定义表单编辑器页面。在表单页面内,我需要有一个 table,我可以在其中列出键值对。属性列表 table 我想创建为可重用组件。
我已经创建了表单,但是在属性 table 组合之前出现不需要的 space(见下图)。我怎样才能摆脱这个空 space?
下面是我的表单页面代码
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.editor.FormPage;
public class EnvironmentPage extends FormPage {
public EnvironmentPage(FormEditor editor) {
super(editor, Activator.PLUGIN_ID + ".amf.environment", "Environment");
}
@Override
protected void createFormContent(IManagedForm managedForm) {
FormToolkit toolkit = managedForm.getToolkit();
ScrolledForm form = managedForm.getForm();
form.setText("Environment");
Composite body = form.getBody();
GridLayout gridLayout = new GridLayout(2, true);
body.setLayout(gridLayout);
toolkit.paintBordersFor(body);
Composite leftComposite = toolkit.createComposite(body, SWT.NONE);
leftComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
GridLayout leftCompositeLayout = new GridLayout();
leftCompositeLayout.marginWidth = 0;
leftCompositeLayout.marginHeight = 0;
leftComposite.setLayout(leftCompositeLayout);
createPropertiesSection(toolkit, leftComposite);
Composite rightComposite = toolkit.createComposite(body, SWT.NONE);
rightComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
GridLayout rightCompositeLayout = new GridLayout();
rightCompositeLayout.marginWidth = 0;
rightCompositeLayout.marginHeight = 0;
rightComposite.setLayout(rightCompositeLayout);
// TODO Controls on left composite
super.createFormContent(managedForm);
}
private void createPropertiesSection(FormToolkit toolkit, Composite leftComposite) {
Section section = toolkit.createSection(leftComposite,
ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED | ExpandableComposite.TWISTIE);
section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
section.setText("Log4J Properties");
Composite log4jComposite = toolkit.createComposite(section, SWT.BORDER);
toolkit.adapt(log4jComposite);
GridLayout gridLayout = new GridLayout();
gridLayout.marginBottom = 0;
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
log4jComposite.setLayout(gridLayout);
log4jComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
section.setClient(log4jComposite);
PropertiesEditor propsEditor = new PropertiesEditor(log4jComposite, SWT.NONE) {
@Override
public void loadData() {
// TODO Auto-generated method stub
}
};
propsEditor.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
toolkit.paintBordersFor(section);
}
}
在属性编辑器下方class
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.widgets.FormToolkit;
public abstract class PropertiesEditor extends Composite {
protected FormToolkit toolkit;
protected Button btnRemove;
protected Button btnAdd;
protected Table propertiesTable;
public PropertiesEditor(Composite parent, int style) {
super(parent, style);
toolkit = new FormToolkit(parent.getDisplay());
createComposite(parent);
}
private void createComposite(Composite parent) {
Composite propertiesGroup = toolkit.createComposite(parent, SWT.BORDER);
GridLayout gridLayout = new GridLayout(2, false);
gridLayout.marginWidth = 1;
gridLayout.marginHeight = 1;
gridLayout.verticalSpacing = 1;
propertiesGroup.setLayout(gridLayout);
propertiesGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
propertiesTable = toolkit.createTable(propertiesGroup, SWT.BORDER | SWT.FULL_SELECTION);
propertiesTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
propertiesTable.setHeaderVisible(true);
propertiesTable.setLinesVisible(true);
propertiesTable.setData("name", "properties-table");
toolkit.adapt(propertiesTable, true, true);
TableColumn tableColumnKey = new TableColumn(propertiesTable, SWT.NONE);
tableColumnKey.setText("Key");
TableColumn tableColumValue = new TableColumn(propertiesTable, SWT.NONE);
tableColumValue.setText("Value");
propertiesTable.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
tableColumnKey.setWidth((int) (propertiesTable.getClientArea().width * 0.3));
tableColumValue.setWidth((int) (propertiesTable.getClientArea().width * 0.7));
}
});
Composite buttonsGroup = toolkit.createComposite(propertiesGroup, SWT.NONE);
buttonsGroup.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true, 1, 1));
GridLayout buttonsLayout = new GridLayout();
buttonsLayout.marginWidth = 10;
buttonsGroup.setLayout(buttonsLayout);
new Label(buttonsGroup, SWT.NONE);
new Label(buttonsGroup, SWT.NONE);
btnAdd = toolkit.createButton(buttonsGroup, "Add", SWT.NONE);
GridData gd_btnAdd = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
gd_btnAdd.widthHint = 60;
btnAdd.setLayoutData(gd_btnAdd);
toolkit.adapt(btnAdd, true, true);
btnRemove = toolkit.createButton(buttonsGroup, "Remove", SWT.NONE);
GridData gd_btnRemove = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
gd_btnRemove.widthHint = 60;
btnRemove.setLayoutData(gd_btnRemove);
toolkit.adapt(btnRemove, true, true);
}
public abstract void loadData();
}
你的 PropertiesEditor
扩展了 Composite
但是你没有对该组合做任何事情,而是在 createComposite
方法中创建另一个 Composite
,其父级是传递的父级到 PropertiesEditor
构造函数。我认为顶部的差距是 PropertiesEditor
复合。