gpt4 book ai didi

java - 最后的 Combo 和 SelectionListener

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:20 26 4
gpt4 key购买 nike

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

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

该怎么做?

如果我取消注释

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

name.addSelectionListener(new SelectionAdapter())

eclipse 要我将 Combo desc 和 Combo name 更改为 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. 您可以在类中创建 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());
    }
    });

关于java - 最后的 Combo 和 SelectionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10739234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com