gpt4 book ai didi

java - CCombo 中的文本选择

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:16 24 4
gpt4 key购买 nike

当用户选择 CCombo 中的项目(或 CCombo 获得焦点)时,字段中的文本将被选中。如何以编程方式删除此选择并将光标位置设置在文本末尾?

最佳答案

您可以添加选择和焦点监听器,并使用 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

关于java - CCombo 中的文本选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41370604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com