单击可编辑的Combobox时,我对文本选择有疑问。
目前发生的情况是,鼠标光标只是到达输入的那个位置而没有选择整个文本字段。我想在点击上突出显示/选择整个文本字段,但它没有这样做。
我想要点击文本字段会发生什么:
这是我的自动完成课程:
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;
}
我在这里创建了ComboBox:
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");
}
}
});
但它只是选择/突出显示文本片刻,然后再次删除突出显示。
答案 0 :(得分:1)
您应该尝试通过在文本字段中添加焦点侦听器来执行此操作:
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 ,对于以前的版本,相应的代码为:
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();
}
}
);
}
}
}
);