如何在javafx tableview中使用特定单词在特定单元格中执行自动建议?

时间:2018-06-01 06:45:59

标签: javafx

我如何使用此代码或如何将此代码放在可编辑的tableview单元格中以获取我的项目中的自动建议功能?以下代码在javafx上正常工作并提供单词的自动建议但我想知道如何在我的项目中应用此代码,我希望在javafx的tableview编辑单元格中自动提示单词?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package autop;

import java.net.URL;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import org.controlsfx.control.textfield.TextFields ;
import org.controlsfx.control.textfield.AutoCompletionBinding ;


/**
 *
 * @author Pragati(Guddu)
 */
public class FXMLDocumentController implements Initializable {

    private Label label;
    @FXML
    private AnchorPane root;
    @FXML
    private TextField input;


    Set<String> possibleWordSet= new HashSet<>();//initialising set
    private AutoCompletionBinding<String> autoCompletionBinding;//global declaration of autocompletionbinding.

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        //String[] possibleWords = {"hey","hello"};

        autoCompletionBinding =  TextFields.bindAutoCompletion(input, possibleWordSet);
        input.setOnKeyPressed((KeyEvent e)->{
            switch(e.getCode())
            {
                case ENTER:
                    learnword(input.getText());//calls the function define below
                    break;
                default:
                    break;
            }
        });
       // TextField textField = TextFields.createClearablePasswordField();
      //  root.getChildren().add(textField);
      //  AnchorPane.setBottomAnchor(textField, 20d);
      //  AnchorPane.setLeftAnchor(textField,20d);

    }    

    private void learnword(String text) {
        possibleWordSet.add(text);//on enter word will go to set
        if(autoCompletionBinding!=null){
            autoCompletionBinding.dispose();
        }
        autoCompletionBinding =  TextFields.bindAutoCompletion(input, possibleWordSet);//it will give auto suggestion

            }

}

0 个答案:

没有答案