最终的Combo和SelectionListener

时间:2012-05-24 14:13:33

标签: java swt drop-down-menu final

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

问题如下,我有一个子窗口,包含一些标签,按钮和两个下拉菜单。我想在一个组合框上有一个SelectionListener,名为'name',这样下拉列表中的选定值将影响第二个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());
        }
    });
}

1 个答案:

答案 0 :(得分: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. 您只需将最终变量设置为descname

    final Combo descRef = desc;
    final Combo nameRef = name;
    
    nameRef.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e)
            descRef.select(nameRef.getSelectionIndex());
        }
    });