2014-04-04 108 views
1

我有一個可編輯的SWT組合。用戶可以從下拉列表中選擇一個項目或輸入新的文本。我的代碼也做了一些數據驗證,如果輸入的文本無效,我想強調焦點到組合以及突出顯示輸入的文本。SWT可編輯組合 - 高亮文本

我的問題是:有沒有辦法突出顯示組合中的所有輸入文本?不幸的是,我認爲沒有「selectText」方法...

在此先感謝。

我試圖巴茲的建議,但不知什麼原因,該代碼同時工作在一個單獨的測試項目,同樣的代碼並不在我的計劃工作:

comboViewerRes = new ComboViewer(this, SWT.DROP_DOWN); 
    comboRes = comboViewerRes.getCombo(); 
    comboRes.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 
      1, 1)); 

    comboRes.addListener(SWT.KeyUp, new Listener() { 
     @Override 
     public void handleEvent(Event e) { 
      if (e.keyCode == SWT.SPACE) { 
       Rectangle bounds = comboRes.getBounds(); 
       Point point = new Point(bounds.x, bounds.x + bounds.width); 
       System.out.println(point); 
       comboRes.setSelection(point); 
      } 
     } 
    }); 
+0

發佈您嘗試使用的代碼。有些人會糾正它並回答 –

回答

1

好了,有做沒有直觀的方式但是,這個Combo包含一個稱爲setSelection(Point)的方法。這種方法的javadoc的規定:

設置在接收器中的文本字段的選擇以通過其x座標是該選擇的開始和其y座標是該選擇的端部的參數指定的範圍內。

爲了選擇一切,你需要做的就是創建一個Point,設置它的x價值的Combo的左邊緣和y協調到Combo的右邊緣。

下面是一個代碼示例:

public static void main(String[] args) 
{ 
    final Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setLayout(new FillLayout()); 
    shell.setText("ExpandBar Example"); 

    final ComboViewer viewer = new ComboViewer(shell, SWT.DROP_DOWN); 

    viewer.getCombo().addListener(SWT.KeyUp, new Listener() 
    { 
     @Override 
     public void handleEvent(Event e) 
     { 
      if(e.keyCode == SWT.SPACE) 
      { 
       Rectangle bounds = viewer.getCombo().getBounds(); 
       Point point = new Point(bounds.x, bounds.x + bounds.width); 
       viewer.getCombo().setSelection(point); 
      } 
     } 
    }); 

    viewer.setContentProvider(ArrayContentProvider.getInstance()); 
    viewer.setLabelProvider(new LabelProvider() 
    { 
     @Override 
     public String getText(Object element) 
     { 
      if (element instanceof String) 
      { 
       return element.toString(); 
      } 
      return super.getText(element); 
     } 
    }); 

    String[] persons = new String[] { "this", "is", "a", "test" }; 

    viewer.setInput(persons); 

    shell.pack(); 
    shell.open(); 

    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
     { 
      display.sleep(); 
     } 
    } 
    display.dispose(); 
} 

可以正常輸入。一旦你按空間,代碼將選擇到目前爲止輸入的所有內容。

下面是一些截圖:

enter image description here enter image description here

+0

該代碼本身工作正常。但是,相同的代碼在使用ComboViewer的代碼中不起作用。我嘗試了'Combo combo = comboViewer.getCombo();'然後按照你的建議添加了監聽器 - 在運行時沒有任何事情發生。 'combo.setSelection'這一行實際上是運行的(我放了一個'System.out.println(bounds);'來確認,我不確定它爲什麼在我的程序中不起作用。這可能是ComboViewer的「特性」 ? – user3229864

+0

@ user3229864那麼,你沒有在你的問題中指定你正在使用'ComboViewer'而不是'Combo',我會研究它 – Baz

+0

@ user3229864更新的代碼它現在使用'ComboViewer' 。相同的代碼仍然可以正常工作。 – Baz

0

其實,我發現是,combo.setSelection()應該採取文本字符串作爲第一個參數的起始索引的一個點,而第二個參數爲結束索引,即在文本中選擇前兩個字符做,combo.setSelection(new Point(0,2));並選擇所有文本做combo.setSelection(new Point(0, combo.getText().length()));

爲什麼它的工作對巴茲和不user3229864,可能是事實,是b az的組合在座標0上,而user3229864不是。

希望這會有所幫助!