在我的可编辑ComboBox中,由于输入过滤了我的建议,我可以触发Button
个事件。问题是,如果我在可编辑字段中键入我的项目名称之一(如action3
)并单击第一个建议(也是action3
),这是合适的项目,它始终使用第一个索引action1
触发ButtonEvent,因为它具有第一个索引,但第一个索引是action1
。
List<EventHandler<ActionEvent>> handlers = Arrays.asList(
this::action1,
this::action2,
this::action3
);
那么如何将action1与我的建议正确建议联系起来?
public void onEnter(ActionEvent event){
int index = editableComboBox.getSelectionModel().getSelectedIndex();
if (index >= 0) {
handlers.get(index).handle(event);
}
}
编辑1:
我的新initialize()方法如下所示:
protected void initialize() {
new AutoCompleteBox<>(autoBox);
ObservableList<ActionEventHandler> data = FXCollections.observableArrayList(
new ActionEventHandler("action1", this::action1),
new ActionEventHandler("action2", this::action2,
new ActionEventHandler("action3", this::action3)
);
autoBox.getItems().setAll(data);
FilteredList<ActionEventHandler> filtered = new FilteredList<>(data);
ComboBox<ActionEventHandler> autoBox = new ComboBox<>(filtered);
autoBox.setOnAction(event -> {
ActionEventHandler h = autoBox.getValue();
if (h != null) {
h.handle(event);
}
});
autoBox.setEditable(true);
autoBox.setConverter(new StringConverter<ActionEventHandler>() {
@Override
public String toString(ActionEventHandler object) {
return object == null ? "" : object.toString();
}
@Override
public ActionEventHandler fromString(String string) {
if (string == null) {
return null;
}
for (ActionEventHandler h : data) {
if (string.equals(h.toString())) {
return h;
}
}
return null;
}
});
autoBox.getEditor().textProperty().addListener((observable, oldValue, newValue) -> {
filtered.setPredicate(h -> h.toString().startsWith(newValue));
});
}
自定义ComboBox
类:
public class AutoCompleteBox<T> implements EventHandler<KeyEvent> {
private ComboBox comboBox;
private StringBuilder sb;
private ObservableList<T> data;
private boolean moveCaretToPos = false;
private int caretPos;
public AutoCompleteBox(final ComboBox comboBox) {
this.comboBox = comboBox;
sb = new StringBuilder();
data = comboBox.getItems();
this.comboBox.setEditable(true);
this.comboBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
comboBox.hide();
}
});
this.comboBox.setOnKeyReleased(AutoCompleteBox.this);
}
@Override
public void handle(KeyEvent event) {
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(
AutoCompleteBox.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;
}
}
我无法将这些功能(例如action1
)与字符串action1
相关联,或者我更相信,我无法将自定义类与我的自定义{{1 }}
答案 0 :(得分:2)
在这种情况下,似乎最好使用EventHandler<ActionEvent>
s作为返回toString
字符串的项目。添加converter
以将项目转换为非String
对象。
public class ActionEventHandler implements EventHandler<ActionEvent> {
private final EventHandler<ActionEvent> eventHandler;
private final String name;
public ActionEventHandler(String name, EventHandler<ActionEvent> eventHandler) {
Objects.requireNonNull(name);
Objects.requireNonNull(eventHandler);
this.name = name;
this.eventHandler = eventHandler;
}
@Override
public String toString() {
return name;
}
@Override
public void handle(ActionEvent event) {
eventHandler.handle(event);
}
}
ObservableList<ActionEventHandler> data = FXCollections.observableArrayList(
new ActionEventHandler("action1", this::action1),
new ActionEventHandler("action2", this::action2),
new ActionEventHandler("action3", this::action3)
);
FilteredList<ActionEventHandler> filtered = new FilteredList<>(data);
ComboBox<ActionEventHandler> comboBox = new ComboBox<>(filtered);
comboBox.setOnAction(event -> {
ActionEventHandler h = comboBox.getValue();
if (h != null) {
h.handle(event);
}
});
comboBox.setEditable(true);
comboBox.setConverter(new StringConverter<ActionEventHandler>() {
@Override
public String toString(ActionEventHandler object) {
return object == null ? "" : object.toString();
}
@Override
public ActionEventHandler fromString(String string) {
if (string == null) {
return null;
}
for (ActionEventHandler h : data) {
if (string.equals(h.toString())) {
return h;
}
}
return null;
}
});
comboBox.getEditor().textProperty().addListener((observable, oldValue, newValue) -> {
filtered.setPredicate(h -> h.toString().startsWith(newValue));
});
修改强>
以下代码应与您的AutoCompleteBox
类一起使用。
@FXML
private ComboBox<ActionEventHandler> autoBox;
@FXML
private void initialize() {
autoBox.getItems().setAll(
new ActionEventHandler("action1", this::action1),
new ActionEventHandler("action2", this::action2),
new ActionEventHandler("action3", this::action3));
autoBox.setOnAction(event -> {
ActionEventHandler h = autoBox.getValue();
if (h != null) {
h.handle(event);
}
});
autoBox.setConverter(new StringConverter<ActionEventHandler>() {
@Override
public String toString(ActionEventHandler object) {
return object == null ? "" : object.toString();
}
@Override
public ActionEventHandler fromString(String string) {
if (string == null) {
return null;
}
for (ActionEventHandler h : autoBox.getItems()) {
if (string.equals(h.toString())) {
return h;
}
}
return null;
}
});
new AutoCompleteBox<>(autoBox);
}
(虽然我没有查看AutoCompleteBox
课程的详细信息...)
答案 1 :(得分:0)
尝试获取项目的字符串并检查字符串内容。如果它的&#34; action3&#34;你将事件称为action3。代码:(String) cbKategorieMain.getSelectedItem()
会将组合框的内容作为字符串