2011-07-29 81 views
0

我在eclipse RCP應用程序中遇到了SWT組合問題。Windows 7 SWT COMBO問題

我會嘗試用一個用例來解釋我的問題,以便更好地理解。

  1. 我有一個Eclipse RCP的視圖組合框與價值說 「A」, 「B」, 「C」, 「d」 和我有它&一個SWT表旁的提交按鈕右側下方。

  2. 一旦在Combo中更改了值並單擊了提交按鈕,則記錄將顯示在表中。

  3. 讓我們假設默認情況下選擇「A」,視圖調用表中顯示A的記錄。

  4. 現在我從下拉列表中選擇「B」並點擊提交。雖然組合顯示「B」,但我只能看到表格中「A」的記錄。

  5. 只有當我再次從組合中選擇「B」,然後點擊提交,「B」的記錄纔會顯示出來。

  6. 現在,如果我從組合中選擇C,只會顯示「B」的記錄。

  7. 後來,如果我從組合中選擇D,則會顯示「C」的記錄。

似乎只處理和顯示前面的選擇而不是當前選擇。

我沒有在Windows XP或prev版本的Windows中遇到這個問題。我最近轉向了Windows 7 64位操作系統,在那裏我遇到了這個問題。

這是一個已知的問題?任何幫助,將不勝感激。

+0

你能顯示與組合和按鈕相關的代碼嗎? –

+0

當然。這裏是一個例子。我點擊提交打印所選索引的索引。請試用Windows 7中的示例。我已將示例上傳到文件共享svr。該文件的鏈接是[鏈接] http://www.4shared.com/file/7NJv6Wge/TestCombo.html – Santhosh

+0

我無法下載文件... –

回答

0


請在下面找到

import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.SelectionAdapter; 
import org.eclipse.swt.events.SelectionEvent; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Combo; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Event; 
import org.eclipse.swt.widgets.Listener; 
import org.eclipse.swt.widgets.Shell; 

public class TestCombo { 


    private static String[] filterByText = new String[] {"A","B","C","D"}; 
    static int index = 0; 

    public static void main(String[] args) { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 


     Composite comp = new Composite(shell, SWT.NONE); 

     GridLayout layout = new GridLayout(2, false); 
     GridData gridData = new GridData(SWT.FILL,SWT.FILL,true,false); 

     comp.setLayout(layout); 
     comp.setLayoutData(gridData); 

     final Combo filter = new Combo (comp, SWT.READ_ONLY); 
     filter.setItems (filterByText); 
     filter.setText (filterByText[0]); 
     filter.setVisibleItemCount(filterByText.length); 

     filter.addListener(SWT.DROP_DOWN, new Listener() { 

      @Override 
      public void handleEvent(Event event) { 
       index = filter.getSelectionIndex(); 

      } 
     }); 

     Button submit = new Button (comp, SWT.PUSH); 
     submit.setText ("Submit"); 
     GridData data = new GridData(); 
     data.widthHint = 80; 
     submit.setLayoutData(data); 
     submit.addSelectionListener (new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent e) { 

       System.out.println("The index is ==> "+index); 
      } 
     }); 
     comp.pack(); 
     shell.pack(); 
     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     display.dispose(); 
    } 

} 
+0

編輯您的原始問題,並在那裏添加代碼。 –

2

示例代碼可以使用下拉監聽器,用於存儲選定的索引。下拉監聽器在組合列表下拉時觸發。此時,存儲舊的選擇。如果您在列表中選擇一個新項目,監聽器將不會再次觸發。儘管稍後您按下提交按鈕,您總能得到先前選定的項目索引。

爲了得到你想要的,你必須使用選擇監聽器而不是下拉監聽器,並且所有工作都正常。選擇偵聽器在您選擇下拉列表中的項目時被調用。只需將SWT.DropDown替換爲SWT.Selection即可。

filter.addListener(SWT.Selection, new Listener() {...});