SWT-如何将数据从一页传输到另一页

时间:2018-08-28 10:07:49

标签: eclipse-plugin swt eclipse-rcp jface

我有一个带有文本框的名为PageOne的WizardPage,我想打印在PageOne的textBox中输入的数据以显示在第二页中。

我尝试将值保存到另一个对象中,然后将该对象传递给PageTwo对象,然后尝试显示,我尝试发送pageOne对象本身。但是在两种情况下,它都将输出NULL。我相信它正在打印出默认值,即在加载页面时加载了null。

如何处理这种情况?

代码:

PageOne:

.link {
    fill: none;
}

PageTwo:

 public void createControl(Composite parent) {
        container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        container.setLayout(layout);
        layout.numColumns = 2;
        Label label1 = new Label(container, SWT.NONE);
        label1.setText("Put a value here.");

        text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
        text1.setText("");
        text1.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (!text1.getText().isEmpty()) {
                    setPageComplete(true);
                }
            }
        });
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        text1.setLayoutData(gd);
        setControl(container);
        setPageComplete(true);
    }

更新

所做的更改:

在向导页面中:

public class PageTwo extends WizardPage {
    private Text text1;
    private Composite container;
    private Text text2;
    PageOne pageOne;

    public PageTwo(PageOne pageOne) {
        super("PageTwo ");
        this.pageOne=pageOne;
        setTitle("PageTwo ");
        setDescription("Fake Wizard: PageTwo");
    }

    public void setText(Text text){
        this.text2=text;
    }

    @Override
    public void createControl(Composite parent) {
        container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        container.setLayout(layout);
        layout.numColumns = 2;
        Label label1 = new Label(container, SWT.NONE);
        String result="";

        if(pageOne.getText()!=null){
            result=pageOne.getText().toString();
        }

        label1.setText("Put a value here."+result);

       /* text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
        text1.setText("");
        text1.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (!text1.getText().isEmpty()) {
                    setPageComplete(true);
                }
            }
        });
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        text1.setLayoutData(gd);*/
        setControl(container);
        setPageComplete(true);
    }

    public String getText1() {
        return text1.getText();
    }
}

DataTransferObject

public void addPages() {
    super.addPages(); 
    DataTransferObject dto = new DataTransferObject();
    addPage(new PageOne(dto));
    addPage(new PageTwo(dto));
    addPage(new PageThree());
}

PageOne:

public class DataTransferObject {

    String data=null;

    public String getData(){
        return this.data;
    }

    public void setData(String data){
        this.data=data;
    }

}

PageTwo:

public class PageOne extends WizardPage {
    private Text text1;
    private Composite container;
    private DataTransferObject dto;

    public Text getText(){
        return this.text1;
    }

    public PageOne(DataTransferObject dto) {
        super("PageOne ");
        setTitle("PageOne ");
        this.dto=dto;
        setDescription("Fake Wizard: PageOne");
    }

    @Override
    public void createControl(Composite parent) {
        container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        container.setLayout(layout);
        layout.numColumns = 2;
        Label label1 = new Label(container, SWT.NONE);
        label1.setText("Put a value here.");

        text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
        text1.setText("");
        text1.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (!text1.getText().isEmpty()) {
                    setPageComplete(true);
                }
            }
        });
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        text1.setLayoutData(gd);
        setControl(container);
        setPageComplete(true);
    }

    public String getText1() {
        return text1.getText();
    }

    @Override
    public IWizardPage getNextPage() {
        // TODO Auto-generated method stub
        System.out.println("Data is "+this.text1.getText().toString());
        dto.setData(this.text1.getText().toString());
        return super.getNextPage();
    }
}

UI部分:

PageOne enter image description here

第二页

enter image description here

1 个答案:

答案 0 :(得分:2)

一种方法是在构造两个页面时将相同的对象传递给两个页面。

您的Wizard可能会这样做:

@Override
public void addPages()
{
  DataTransferObject dto = new DataTransferObject();

  addPage(new PageOne(dto));

  addPage(new PageTwo(dto);
}

向导页面如下:

public class PageOne extends WizardPage {
    DataTransferObject dto;

    public PageOne(DataTransferObject dto) {
        super("PageOne");

        this.dto = dto;
    }
public class PageTwo extends WizardPage {
    DataTransferObject dto;

    public PageTwo(DataTransferObject dto) {
        super("PageTwo ");

        this.dto = dto;
    }

DataTransferObject是您编写的某个类,其中包含要共享的数据。 PageOne应该在对象中设置值,以便PageTwo可以检索它们。

PageTwo应该等待setVisible调用来访问数据:

 @Override
 public void setVisible(final boolean visible)
 {
   if (visible)
    {
      // TODO access data and update the controls which show the data
    }
 }

super.setVisible(visible);

}

setVisible代码必须更新已经创建的控件。因此,对于Text控件,您可以调用setText,依此类推。