Eclipse SWT Widget:结合两个监听器

时间:2017-10-02 16:59:41

标签: java eclipse eclipse-plugin swt

我目前正在更新向导页面,我希望在选择textField后禁用radioButton

例如:if(radioButton [1] .getSelection)
          "然后让我们说textField [2] .setEnabled(false)"

这里的问题是,如何将radioButton[i]textField[i]的两个听众连接在一起?感谢。

 case composed:
            container.setLayout(new RowLayout());
            Button[] radioButton= new Button[answers.size()];
            for (int i = 0; i < answers.size(); i++) {
                String ans = answers.get(i).getValue();
                radioButton[i]=new Button(container, SWT.RADIO);
                radioButton[i].setText(ans);
                radioButton[i].addSelectionListener(new SelectionAdapter(){

                    @Override
                    public void widgetSelected(SelectionEvent arg0) {
                        // TODO Auto-generated method stub

                    }});
            }
            Text[] textField=new Text[answers.size()];
            for (int i = 0; i < answers.size(); i++) {
                textField[i]=new Text(container, SWT.SINGLE | SWT.BORDER);
                textField[i].addModifyListener(new ModifyListener(){

                    @Override
                    public void modifyText(ModifyEvent arg0) {
                        // TODO Auto-generated method stub

                    }

                });
            }

1 个答案:

答案 0 :(得分:3)

在添加侦听器之前创建单选按钮和文本字段,以便您可以引用它们。

        Button[] radioButton = new Button[answers.size()];
        for (int i = 0; i < answers.size(); i++) {
            String ans = answers.get(i).getValue();
            radioButton[i] = new Button(container, SWT.RADIO);
            radioButton[i].setText(ans);
        }

        Text[] textField = new Text[answers.size()];
        for (int i = 0; i < answers.size(); i++) {
            textField[i] = new Text(container, SWT.SINGLE | SWT.BORDER);

            radioButton[i].addSelectionListener(new SelectionAdapter(){
                @Override
                public void widgetSelected(SelectionEvent arg0) {
                  textField[i].setEnabled(radioButton[i].getSelection());
                }});

            textField[i].addModifyListener(new ModifyListener(){
                @Override
                public void modifyText(ModifyEvent arg0) {
                  // your stuff
                }});
        }

您还可以使两个侦听器成为相同的侦听器对象(但老实说,为什么要这么做 - 让单个用途的小型侦听器变得更好):

private static class MyListener extends SelectionAdapter implements ModifyListener {
     public void widgetSelected(SelectionEvent arg) { ... }
     public void modifyText(ModifyEvent arg) { ... }
}