gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:32:40 25 4
gpt4 key购买 nike

我正在通过 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");
}
}

最佳答案

addDataToForm 中调用 layout(true, true) 来让 Composite 重做其布局,同时考虑新内容。

关于java - 如何在 createPartControl 之后添加更多控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37866897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com