2013-01-21 41 views
1

我有一個代碼添加了四個複選框按鈕到SWT外殼,我添加了選擇監聽器到checkBox按鈕,我想要刪除點擊確定,取消和關閉按鈕SWT殼牌,我該怎麼處理這樣的事件,這是我的代碼處理SelectionListener在SWT上點擊另一個按鈕

grpVersionTreeComponents.setLayoutData(componentsRenderer.createGridData(490, 220, 4)); 
      for(int versionCount = 0; versionCount < versionSplitters.length ; versionCount++){ 
       String splitter= versionSplitters[versionCount]; 
       Button cbVersionSplitter = new CheckBoxWrapper().getButton(grpVersionTreeComponents,splitter.toString() , ""); 
       cbVersionSplitter.setEnabled(true); 
       versionSplitterCheckBoxList.add(cbVersionSplitter); 
       versionSplitterCheckBoxList.get(versionCount).addSelectionListener(addSplitterCheckBoxListner(cbVersionSplitter)); 

      } 

這是我的選擇Adapter方法

public SelectionAdapter addSplitterCheckBoxListner(final Button button){ 
     return new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent e) { 


       String[] defaultVersionSplitters = PropertyClass.getPropertyLabel(QTLConstants.VERSIONING_ASSISTENT_PAGE_SPLLITER).split(QTLConstants.MULTIPLE_EXTENSIONS_SPLITER); 
       //check if button.getText() is equal to "Custom Splitter" String 
       if(button.getText().equalsIgnoreCase(defaultVersionSplitters[3])){ 
        if(button.getSelection()){ 
         customVersionSplitterText.setEnabled(true); 
        }else{ 
         customVersionSplitterText.setEnabled(false); 
        } 

       } 


      } 

     }; 
    } 

和確定取消按鈕以這種方式加入

private void addOkCancelButtonOnVersionTreePopup(final Shell versionTreeComponentsShell){ 

     Button ok = componentsRenderer.createButtonWidget(versionTreeComponentsShell, SWT.PUSH, 
       PropertyClass.getPropertyLabel(QTLConstants.OK_BUTTON)); 
     Button cancel = componentsRenderer.createButtonWidget(versionTreeComponentsShell, SWT.PUSH, 
       PropertyClass.getPropertyLabel(QTLConstants.CANCEL_BUTTON)); 
     ok.addSelectionListener(new SelectionAdapter() { 
      @Override 
      public void widgetSelected(SelectionEvent e) { 

} 
     }); 
} 
cancel.addSelectionListener(new SelectionAdapter() { 
      @Override 
      public void widgetSelected(SelectionEvent e) {   
       /*disposeSelectionListener(cbVersionSplitter);*/ 
       versionTreeComponentsShell.close();    
      } 
     }); 

    } 
} 

所以我怎樣才能消除這種選擇監聽器

問候

回答

0

你可以做到以下幾點:

1)。創建將擴展SelectionAdapter類的類。 2)。重寫widgetSelected()方法,就像你在代碼中完成的那樣。 3)。覆蓋equals(Object other)方法有以下幾點:

@Override 
public boolean equals(Object other) { 
    if (other == null) { 
     return false; 
    } 
    if (other instanceof MyOwnSelectionAdapter) { 
     return true; 
    } 
} 

其中MyOwnSelectionAdapter是自己實現SelectionAdapter。 4)。現在您可以執行以下操作:cancel.removeSelectionListener(new MyOwnSelectionAdapter());,它將刪除該選擇偵聽器。