如何修复自动完成组合框中的清除功能和插入符号位置

时间:2019-04-17 11:58:18

标签: java javafx

我一直在实现自动完成组合框,直到现在它已成功显示匹配的结果,但是问题是我无法清除所选的项目。另一个问题是,插入符默认位置更改为第一个字符之前,因为我希望插入符号位于最后一个字符之后。 建议任何解决方案

public class AutocompleteController {

    @FXML
    ComboBox<String> autoCompleteComboId;
    @FXML
    Button okButtonId;
    @FXML
    TextField textFieldId;

    List<String> itemList = null;
    ObservableList<String> observableList =  FXCollections.observableArrayList();
    String text = null;
    String updatedText;
    EventHandler<KeyEvent> keyEvent;

@FXML
    private void initialize() {
        observableList = FXCollections.observableArrayList(buildData());
        autoCompleteComboId.setEditable(true);
        autoCompleteComboId.setVisibleRowCount(3);

        autoCompleteComboId.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent t) {
                autoCompleteComboId.hide();
            }
        });
        autoCompleteComboId.setOnKeyReleased(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                // TODO Auto-generated method stub
                autoCompleteComboId.show();
            }
        });
       bindEventProcessing();
}
    public void bindEventProcessing() {
        TextField editor = autoCompleteComboId.getEditor();
        keyEvent = new EventHandler<KeyEvent>() {
              @Override
            public void handle(KeyEvent event) {
                //autoCompleteComboId.getSelectionModel().clearSelection();
                ObservableList<String> items = FXCollections.observableArrayList(buildData());
                ObservableList<String> filteredObservableList = FXCollections.observableArrayList();

                if (autoCompleteComboId.getEditor().getText().length() > 0) {
                    System.out.println("current editor text " + autoCompleteComboId.getEditor().getText());
                    for (int i = 0; i < items.size(); i++) {
                        if (items.get(i).startsWith(autoCompleteComboId.getEditor().getText())) {
                            System.out.println("matched  " + autoCompleteComboId.getEditor().getText());
                            filteredObservableList.add(items.get(i));
                        } else {
                            System.out.println("match not found");
                        }
                    }
                } else {
                    System.out.println("editor is empty");
                }
      System.out.println("Filtered List =>" + filteredObservableList.size());
                if (!(filteredObservableList.isEmpty())) {
                    autoCompleteComboId.setItems(filteredObservableList);
                    autoCompleteComboId.show();
                }

                if (!(autoCompleteComboId.getEditor().getText().length() > 0)) {
                    System.out.println("Editor is null");
                    autoCompleteComboId.setItems(observableList);
                    autoCompleteComboId.show();
                } else {
                    System.out.println("items not found");
                }
                }
        };

        editor.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent);
         }
   }

0 个答案:

没有答案