2012-05-24 54 views
3

您好強大的編程人員,再次作爲初學者的n00b我要求你的幫助......最終組合和SelectionListener中

問題如下,我有一個子窗口,包含一些標籤,按鈕和兩個下拉菜單。我想在一個名爲'name'的組合框上有一個SelectionListener,這樣從這個下拉列表中選擇的值將影響第二個組合框中的選定值。

如何做到這一點?

如果我取消

name.addSelectionListener(new SelectionAdapter()) 

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

日食要我改變組合遞減和組合名決賽,這是不可能的因爲真的需要初始化它們在循環外毀了我佈局:(

這裏是代碼:

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
  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()); 
        } 
    }); 
    
+0

非常感謝你多:) – mentis