我想在编辑tableview单元格文本字段后,当我输入我想要自动提示单词时

时间:2018-05-29 09:59:38

标签: javafx-8

此代码基本上包含如何编辑tableview列的文本字段,如tableview列是textfield格式,我需要编辑它,当我输入我需要自动建议但我的自动建议代码不起作用,所以任何人都可以建议我或帮助用相同的程序克服我的单词问题的自动建议,在commitedit之后我也称为learnword函数,但是没有发生。

class EditingCell extends TableCell<File, String> {
         Set<String> possibleWordSet= new HashSet<>();
        private AutoCompletionBinding<String> autoCompletionBinding;


        public TextField textField;
        //private Object possibleWordSet;

        @Override
        public void startEdit() {

            if (!isEmpty()) {
                super.startEdit();
                createTextField();
                setText(null);
                setGraphic(textField);
                System.out.println(textField.getCaretPosition() + "caret position");

            }
        }

        @Override
        public void cancelEdit() {
            super.cancelEdit();
            setText(getItem());
            setGraphic(null);
        }

        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);

            if (empty) {
                System.out.println("I am in update function");
                setText(null);
                setGraphic(null);
            } else {
                if (isEditing()) {
                    if (textField != null) {
                        textField.setText(item);
                    }
                    setText(null);
                    setGraphic(textField);
                } else {
                    setText(item);
                    setGraphic(null);
                }
            }
        }

        // Instantiates the text field.
        public void createTextField() {

            textField = new TextField(getItem());
            System.out.println("hello i am inside the createtextfield");


            textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
                    autoCompletionBinding =  TextFields.bindAutoCompletion(textField, possibleWordSet); /tion/for auto suuges

            textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent t) {
                    if (t.getCode() == KeyCode.ENTER) {

                        commitEdit(textField.getText());//on  enter event editing of textfield
                         learnword(textField.getText());//for auto suggestion call learnword method.

                    } else if (t.getCode() == KeyCode.ESCAPE) {
                        cancelEdit();
                    }
                }
            });


            textField.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    int caretPosition = textField.getCaretPosition();
                    GUIFXMLDocumentController fx = new GUIFXMLDocumentController();
                    fx.setCaretPos(caretPosition);
                    System.out.println(caretPosition + "caretPosition");
                    fx.setTextField(textField);
                }
            });
            textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) {
                    if (!arg2) {
                        System.out.println(textField.getText() + "Inside changeListener------------------");
                        GUIFXMLDocumentController.tv_new.getFocusModel().focusNext();
                        commitEdit(textField.getText());
                         learnword(textField.getText());

                    }

                }
            });


        }
                 public void learnword(String text) {
            possibleWordSet.add(text);
            if(autoCompletionBinding!=null){
                autoCompletionBinding.dispose();
            }
            autoCompletionBinding =  TextFields.bindAutoCompletion(textField, possibleWordSet);




    }
    }

    enter code here

0 个答案:

没有答案