简体   繁体   English

如何在JFace向导中动态创建控件

[英]How to create controls dynamically in JFace Wizard

I have a jFace wizard, I am using this to create a new project type eclipse plugin. 我有一个jFace向导,正在使用它创建一个新的项目类型eclipse插件。 As you can see from image below, I have one treeviewer on left side, and a SWT group on right side. 从下图可以看到,左侧有一个treeviewer,右侧有一个SWT组。 What I want is when ever user selects one of the item from treeviewer, I should be able to create dynamic controls on right side SWT Group. 我想要的是,当用户从treeviewer中选择一项时,我应该能够在右侧的SWT Group上创建动态控件。 Say user selects Test One, one right side I should be able to create few controls like label, text and few radio buttons on right side, similarly if user selects Test Two I should be able to create dynamic controls on right side. 假设用户选择了“测试一”,那么我应该能够在右侧创建一些控件,例如标签,文本和几个单选按钮。类似地,如果用户选择“测试二”,我应该能够在右侧创建动态控件。 在此处输入图片说明

Currently I tried below code: 目前,我尝试了以下代码:

tree.addSelectionListener(new SelectionAdapter() {
     @Override
 public void widgetSelected(SelectionEvent e) {
     for (int i = 0; i < selection.length; i++) {

     String tempStr = selection[i].toString();
     tempStr = tempStr.replaceAll("TreeItem \\{", "");
     String finalStr = tempStr.replaceAll("\\}", "");

         if (finalStr.equals("Test One")) {
             Button btn = new Button(g2, SWT.NONE); //g2 is right side group

             btn.setText("Blaaaa");

             btn.setVisible(true);
             container.redraw();
         }

}

But when I run, I see no changes on right group. 但是当我跑步时,我看不到右侧组的任何变化。 Can anyone guide me what I am doing wrong? 谁能指导我我做错了什么? Any pointers would be very appreciated, since I am new to Eclipse development and SWT. 因为我是Eclipse开发和SWT的新手,所以任何指针都将不胜感激。

You probably didn't set a layout on the g2 group. 您可能没有在g2组上设置布局。 This is the common cause for controls not showing up. 这是控件未显示的常见原因。 You can also try using g2.layout() to ensure that the new controls are correctly laid out after you create them. 您也可以尝试使用g2.layout()来确保在创建新控件后正确放置它们。

Additionally you could look at using a StackLayout so that once you create a set of controls you can just hide them all at once instead of destroying when the selection changes. 另外,您可以考虑使用StackLayout这样,一旦创建了一组控件,就可以一次隐藏所有控件,而不必在选择更改时销毁它们。 This is often useful so that if the user comes back to a previous selection, they will find the data they entered in the same state when they switched the selection. 这通常很有用,因此,如果用户返回到先前的选择,则当他们切换选择时,他们会发现以相同状态输入的数据。 Here is an example . 这是一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM