问题描述
我有一个关于单击可编辑组合框时选择文本的问题.
I have a question regarding text selection when clicking on the editable Combobox.
目前发生的情况是,鼠标光标只是移动到输入的那个位置,并没有选择整个文本字段.我想在点击时突出显示/选择整个文本字段,但它没有这样做.
Currently what happens, is that the mouse cursor just goes to that place of the input and does not select the whole text field. I would like to highlight/select whole textfield on the click, but it doesn't do that.
我想要点击文本字段会做什么:
What I want that a click on the textfield would do:
这是我的自动完成类:
public class AutoCompleteComboBoxListener<T> implements EventHandler<KeyEvent> {
private ComboBox comboBox;
private StringBuilder sb;
private ObservableList<T> data;
private boolean moveCaretToPos = false;
private int caretPos;
private Button button;
public AutoCompleteComboBoxListener(final ComboBox comboBox, final Button button) {
this.comboBox = comboBox;
sb = new StringBuilder();
data = comboBox.getItems();
this.comboBox.setEditable(true);
this.button = button;
this.comboBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
System.out.println("misaja");
System.out.println(t);
comboBox.hide();
}
});
this.comboBox.setOnKeyReleased(AutoCompleteComboBoxListener.this);
}
@Override
public void handle(KeyEvent event) {
System.out.println("nonoh");
System.out.println(event.getCode());
UI.bitLogger.logging(String.valueOf(event.getCode()));
if (event.getCode() == (KeyCode.ENTER)) {
System.out.println("erki");
button.fire();
}
if (event.getCode() == KeyCode.UP) {
caretPos = -1;
moveCaret(comboBox.getEditor().getText().length());
return;
} else if (event.getCode() == KeyCode.DOWN) {
if (!comboBox.isShowing()) {
comboBox.show();
}
caretPos = -1;
moveCaret(comboBox.getEditor().getText().length());
return;
} else if (event.getCode() == KeyCode.BACK_SPACE) {
moveCaretToPos = true;
caretPos = comboBox.getEditor().getCaretPosition();
} else if (event.getCode() == KeyCode.DELETE) {
moveCaretToPos = true;
caretPos = comboBox.getEditor().getCaretPosition();
}
if (event.getCode() == KeyCode.RIGHT || event.getCode() == KeyCode.LEFT
|| event.isControlDown() || event.getCode() == KeyCode.HOME
|| event.getCode() == KeyCode.END || event.getCode() == KeyCode.TAB) {
return;
}
ObservableList list = FXCollections.observableArrayList();
for (int i = 0; i < data.size(); i++) {
if (data.get(i).toString().toLowerCase().startsWith(
AutoCompleteComboBoxListener.this.comboBox
.getEditor().getText().toLowerCase())) {
list.add(data.get(i));
}
}
String t = comboBox.getEditor().getText();
comboBox.setItems(list);
comboBox.getEditor().setText(t);
if (!moveCaretToPos) {
caretPos = -1;
}
moveCaret(t.length());
if (!list.isEmpty()) {
comboBox.show();
}
}
private void moveCaret(int textLength) {
if (caretPos == -1) {
comboBox.getEditor().positionCaret(textLength);
} else {
comboBox.getEditor().positionCaret(caretPos);
}
moveCaretToPos = false;
}
我在这里创建组合框:
final ComboBox comboBox = new ComboBox(options);
comboBox.setPrefWidth(320);
comboBox.setValue("");
//ComboBox comboBox = new ComboBox();
new AutoCompleteComboBoxListener<>(comboBox, goButton);
comboBox.addEventFilter(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent ke) {
if (ke.getCode() == KeyCode.ENTER) {
//System.out.println("ENTER was released");
goButton.fire();
}
}
});
comboBox.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
System.out.println("mouse click detected2! " + mouseEvent.getSource());
//comboBox.getEditor().requestFocus();
//comboBox.getEditor().requestFocus();
//comboBox.requestFocus();
//comboBox.getEditor().selectAll();
//comboBox.requestFocus();
//None of these seem to work.
}
});
我现在添加的是:
comboBox.getEditor().focusedProperty().addListener(new ChangeListener<Boolean>()
{
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
{
if (newPropertyValue)
{
System.out.println("Textfield on focus");
comboBox.requestFocus();
comboBox.getEditor().selectAll();
}
else
{
System.out.println("Textfield out focus");
}
}
});
但它所做的只是选择/突出显示文本片刻,然后再次删除突出显示.
But all it does is that it selects/highlights the text for just a moment and then removes the highlight once again.
推荐答案
您应该尝试通过在文本字段上添加一个焦点侦听器来实现,如下所示:
You should try to do it by adding a focus listener on the textfield as next:
comboBox.getEditor().focusedProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue) {
// Select the content of the field when the field gets the focus.
Platform.runLater(comboBox.getEditor()::selectAll);
}
}
);
此代码适用于 Java 8,对于以前的版本,相应的代码是:
This code is for Java 8, for previous versions the corresponding code is:
comboBox.getEditor().focusedProperty().addListener(
new ChangeListener<Boolean>() {
@Override
public void changed(final ObservableValue<? extends Boolean> observable,
final Boolean oldValue, final Boolean newValue) {
if (newValue) {
Platform.runLater(
new Runnable() {
@Override
public void run() {
comboBox.getEditor().selectAll();
}
}
);
}
}
}
);
这篇关于单击选择 JavaFX 可编辑组合框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!