3

嗨,强大的程序员,我再次作为 n00b 初学者寻求您的帮助......

问题如下,我有一个子窗口,包含一些标签、按钮和两个下拉菜单。我想在一个名为“name”的组合框中有一个 SelectionListener,以便从此下拉列表中选择的值将影响第二个 Combo - desc 中的选择值。

怎么做?

如果我取消注释

//desc.select(name.getSelectionIndex());

name.addSelectionListener(new SelectionAdapter())

eclipse 希望我将 Combo desc 和 Combo 名称更改为 final,这是不可能的,因为它需要在循环外初始化它们,破坏我的布局:(

这是代码:

public void tariffAddWindow(final Shell childWindow) {

    childWindow.setText("add dialog");

    labels = new Label[operatorTariffData.getColumnNames().length];
    inputTexts = new Text[operatorTariffData.getColumnNames().length];
    final Button buttons[] = new Button[operatorTariffData.getColumnNames().length];

    Combo name = null;
    Combo desc = null;

    for (int i =1; i< operatorTariffData.getColumnNames().length; i++) {


            labels[i] = new Label(childWindow, SWT.NONE);
            labels[i].setText(operatorTariffData.getColumnNames()[i].toString());
            labels[i].setBackground(blue);          

        if (i == 3) {
            name = new Combo(childWindow, SWT.READ_ONLY | SWT.BORDER);
            name.setLayoutData(tLayout);
            buttons[i] = new Button(childWindow, SWT.PUSH);
            buttons[i].setText("e");
        } else if (i == 4) {
            desc = new Combo(childWindow, SWT.READ_ONLY | SWT.BORDER);
            desc.setLayoutData(tLayout);
            buttons[i] = new Button(childWindow, SWT.PUSH);
            buttons[i].setText("e");
        } else {

            inputTexts[i] = new Text(childWindow, SWT.None);
            //texts[i].setText(data.getData()[i].toString());
            inputTexts[i].setLayoutData(tLayout);

            buttons[i] = new Button(childWindow, SWT.TOGGLE);
            buttons[i].setText("x");
            buttons[i].setSelection(false);
        }
    }

    for (int i = 1; i < contentProvider.getTariffNames().getQueryRowCount(); i++) {
        name.add(contentProvider.getTariffNames().getQueryData()[i][3].toString());
        desc.add(contentProvider.getTariffNames().getQueryData()[i][2].toString());
    }

    name.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            //desc.select(name.getSelectionIndex());
        }
    });
}
4

1 回答 1

1
  1. 您可以在类中创建descname字段:

    class Foo extends Composite
    {
        Combo name;
        Combo desc;
    
        public void tarriffAddWindow(final Shell childWindow)
        {
            // ...
            name.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e)
                    desc.select(name.getSelectionIndex());
                }
            });
        }
    }
    
  2. 您可以简单地将最终变量设置为descand name

    final Combo descRef = desc;
    final Combo nameRef = name;
    
    nameRef.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e)
            descRef.select(nameRef.getSelectionIndex());
        }
    });
    
于 2012-05-24T14:18:50.413 回答