Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在createPartControl之后添加更多控件?_Java_Eclipse Plugin_Swt - Fatal编程技术网

Java 如何在createPartControl之后添加更多控件?

Java 如何在createPartControl之后添加更多控件?,java,eclipse-plugin,swt,Java,Eclipse Plugin,Swt,我正在通过org.eclipse.ui.views.properties.tabbed制作一个属性表,我有一个部分需要根据选择的元素动态生成属性部分中的字段。问题是我不知道createControls之前所选的元素,并且我无法更改它以使我知道它。如何将字段动态添加到合成中 我正在使用WindowBuilder工具更容易地创建表单的其他部分,尽管我不需要使用它 下面是一个示例,显示了我正在尝试做的事情: Form.java import org.eclipse.swt.SWT; import or

我正在通过
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);
    }
}
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");
    }
}
PropertySection.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);
    }
}
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");
    }
}
调用
addDataToForm
中的
layout(true,true)
以获取
组合
,从而在考虑新内容的情况下重新进行布局。

调用
layout(true,true)
以获取
组合
以在考虑新内容的情况下重新进行布局