2016-12-28 20 views
1

當用戶選擇CCombo(或CCombo獲得焦點)中的項目時,該字段中的文本將被選中。我怎樣才能以編程方式刪除這個選擇並在文本的末尾設置光標位置?CCombo中的文本選擇

回答

0

您可以添加一個選擇和聚焦聽衆和修改有光標位置setSelection,例如:

public static void main(String[] args) { 

    Display display = new Display(); 

    Shell shell = new Shell(display); 
    shell.setSize(300, 150); 
    shell.setLayout(new GridLayout()); 

    CCombo combo = new CCombo(shell, 0); 
    combo.setItems(new String[] { "String 1", "Test", "StackOverflow"}); 

    combo.addSelectionListener(selectionAdapter); 
    combo.addFocusListener(focusAdapter); 

    CCombo combo2 = new CCombo(shell, 0); 
    combo2.setItems(new String[] { "String 1", "Test", "StackOverflow"}); 

    combo2.addSelectionListener(selectionAdapter); 
    combo2.addFocusListener(focusAdapter); 

    shell.open(); 
    while (!shell.isDisposed()) { 
     if (!shell.getDisplay().readAndDispatch()) { 
      shell.getDisplay().sleep(); 
     } 
    } 
} 

static void selectionAtEnd(CCombo c) { 
    // get the length of the selected item 
    String text = c.getText(); 
    int endSelection = text.length(); 

    // set the cursor at the end of the text 
    c.setSelection(new Point(endSelection, endSelection)); 
} 

static SelectionAdapter selectionAdapter = new SelectionAdapter() { 
    @Override 
    public void widgetSelected(SelectionEvent arg0) { 
     // change selection when an item is selected 
     selectionAtEnd((CCombo) arg0.getSource()); 
    } 
}; 

static FocusAdapter focusAdapter = new FocusAdapter() { 
    @Override 
    public void focusGained(FocusEvent arg0) { 
     // change selection when focus is gained 
     selectionAtEnd((CCombo) arg0.getSource()); 
    } 
}; 

結果:

CCombo animation