我正在通过org.eclipse.ui.views.properties.tabbed
创建一个属性表,我有一个部分,我需要根据选择的元素动态生成属性部分中的字段。问题是我在createControls
之前不知道所选元素,我无法更改它以便我知道它。如何动态地向复合材料添加字段?
我正在使用WindowBuilder工具更轻松地创建表单的其他部分,但我不需要使用它。
这是一个显示我正在尝试做的事情的例子:
Form.java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
public class Form extends Composite {
private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
private Text text;
/**
* Create the composite.
* @param parent
* @param style
*/
public Form(Composite parent, int style) {
super(parent, style);
addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
toolkit.dispose();
}
});
toolkit.adapt(this);
toolkit.paintBordersFor(this);
setLayout(new GridLayout(2, false));
Label lblNewLabel = new Label(this, SWT.NONE);
lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
toolkit.adapt(lblNewLabel, true, true);
lblNewLabel.setText("New Label");
text = new Text(this, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
toolkit.adapt(text, true, true);
}
public void addDataToForm(String label) {
Label lblNewLabel = new Label(this, SWT.NONE);
lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
toolkit.adapt(lblNewLabel, true, true);
lblNewLabel.setText(label);
// Don't just want to add Texts; sometimes a checkbox, sometimes something else entirely
Text text = new Text(this, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
toolkit.adapt(text, true, true);
}
}
PropertySection.java
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
public class PropertySection extends AbstractPropertySection {
private Form form;
@Override
public void createControls(Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage) {
super.createControls(parent, aTabbedPropertySheetPage);
TabbedPropertySheetWidgetFactory factory = getWidgetFactory();
Composite composite = factory.createFlatFormComposite(parent);
form = new Form(composite, SWT.NONE);
FormData data = new FormData();
data.left = new FormAttachment(0);
data.right = new FormAttachment(100);
form.setLayoutData(data);
}
@Override
public void refresh() {
// This is where I am able to obtain the info I need to add the elements to the form
// This doesn't work:
form.addDataToForm("My Label");
}
}
答案 0 :(得分:2)
在layout(true, true)
中致电addDataToForm
,让Composite
重新布局,将新内容记入帐户。