【问题标题】:How to dynamically create an SWT Text based on a combo selection如何基于组合选择动态创建 SWT 文本
【发布时间】:2013-06-19 13:16:08
【问题描述】:

我的对话框中有一个 Combo 和一个文本小部件。最初加载对话框时,我只希望组合出现,但基于组合选择,我希望显示文本。

代码如下:

public class NewDialog extends Dialog 
{
private Label label1;
private Combo combo;

private Label label2;
private Text text;

private Composite container;

public NewDialog(Shell parentShell) {
    super(parentShell);
}

@Override
protected Control createDialogArea(Composite parent) 
{
    container = (Composite) super.createDialogArea(parent);
    GridLayout gridLayout = (GridLayout) container.getLayout();
    gridLayout.numColumns = 3;
    new Label(container, SWT.NONE);
    new Label(container, SWT.NONE);
    new Label(container, SWT.NONE);
    new Label(container, SWT.NONE);

    label1 = new Label(container, SWT.NONE);
    label1.setText("Class");

    combo = new Combo(container, SWT.NONE);
    GridData gd_combo = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
    gd_combo.widthHint = 165;
    combo.setLayoutData(gd_combo);
    new Label(container, SWT.NONE);
    new Label(container, SWT.NONE);
    new Label(container, SWT.NONE);
    new Label(container, SWT.NONE);
    new Label(container, SWT.NONE);
    new Label(container, SWT.NONE);
    new Label(container, SWT.NONE);

    combo.add("One");
    combo.add("Two");
    combo.add("Three");


    combo.addSelectionListener(new SelectionAdapter()
    {
        public void widgetSelected(SelectionEvent event)
        {
            if(combo.getText().equals("One"))
            {
                label2 = new Label(container, SWT.NONE);
                label2.setText("Name");


                text = new Text(container, SWT.BORDER);
                GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
                gd_text.widthHint = 239;
                text.setLayoutData(gd_text);
            }

        }
    });

    return container;
}

我没有根据组合选择在对话框中显示文本。

请告诉我我哪里出错了

【问题讨论】:

  • 试试 Comb#getSelectionIndex(),但也许 Combo 不是正确的控件?
  • @kk331317 对您得到的答案有任何反馈吗?

标签: java eclipse eclipse-plugin swt jface


【解决方案1】:

此代码将完成这项工作:

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, false));

    final Combo combo = new Combo(shell, SWT.SINGLE);
    combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

    combo.add("First");
    combo.add("Second");
    combo.add("Third");

    Label label = new Label(shell, SWT.NONE);
    label.setText("Label: ");
    final GridData labelData = new GridData(SWT.BEGINNING, SWT.CENTER, false, true);
    labelData.exclude = true;
    label.setLayoutData(labelData);

    Text text = new Text(shell, SWT.BORDER);
    final GridData textData = new GridData(SWT.FILL, SWT.FILL, true, true);
    textData.exclude = true;
    text.setLayoutData(textData);

    combo.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            boolean change = !combo.getText().equals("First");
            labelData.exclude = change;
            textData.exclude = change;
            shell.pack();
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

它基本上使用GridData.exclude 字段来隐藏/显示LabelText

【讨论】:

    【解决方案2】:

    如果您确实需要动态创建新控件,则应在之后调用父控件上的layout()。但是,在这种情况下,使控件可见/不可见(如@Baz 的回答)会更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多