JavaFX - 在TextArea

时间:2015-09-04 17:32:00

标签: java string javafx textarea

我正在尝试将当前正在输入JavaFX TextArea的单词或部分单词传递给一个非常基本的AutoComplete getPredictions(String前缀);方法。

现在我在KeyTyped上使用事件处理程序,我将跟踪每个键入的字符,并将其与之前输入的单词连接,重置空格。在键入一个单词的过程中打开AutoComplete时,我会看到很多陷阱,如果触发退格并且插入符号再次位于单词的末尾,则会获得前一个单词。

我很想知道是否有一种替代方法,任何人都可以想到我只是使用TextInputControl方法从插入符号位置获取字符串返回到最后一个空格。我还没有按照我的方式工作,但我会在此时发布我的内容。

我基本上需要输入当前的单词,保留当前插入位置作为使用类型。

public AutoSpellingTextArea() {

    // register and handle KEY_TYPED event
    this.addEventHandler(KeyEvent.KEY_TYPED,new EventHandler<KeyEvent>(){

        public void handle(KeyEvent t) {

            // BUG -- turning on autocomplete in middle of word.
            if(autoCompleteOn) {
                //TODO
                String pre = getCurrentPrefix(t.getCharacter());

                if(pre != null) {
                    List<String> choices = ac.predictCompletions(pre, NUM_COMPLETIONS);
                }
            }
        }

    });
}


private String getCurrentPrefix(String ch) {
    String retVal = null;

    // space entered
    if(ch.equals(" ")) {
        lastWord = currentPrefix;
        currentPrefix = "";
        return retVal;
    }

    // add next character to prefix string
    currentPrefix = currentPrefix + ch;

    return retVal;

}

1 个答案:

答案 0 :(得分:3)

也许你可以得到你需要的只是观察caratPositionProperty并回溯到最后一个空格字符:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class AutoCompleteTextAreaTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextArea textArea = new TextArea();
        Label currentWord = new Label();

        textArea.caretPositionProperty().addListener((obs, oldPosition, newPosition) -> {
            String text = textArea.getText().substring(0, newPosition.intValue());
            int index ;
            for (index = text.length() - 1; index >= 0 && ! Character.isWhitespace(text.charAt(index)); index--);
            String prefix = text.substring(index+1, text.length());
            currentWord.setText(prefix);
        });


        BorderPane root = new BorderPane(textArea, currentWord, null, null, null);
        primaryStage.setScene(new Scene(root, 600, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

您可以将此想法扩展为从克拉位置向前搜索,这样,如果光标位于单词的中间,您可以识别可能因插入该点而导致的所有可能单词。

这是一个SSCCE使用我在网上找到的随机单词列表来证明这一点:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class AutoCompleteTextAreaTest extends Application {

    private static final String WORD_LIST_URL = "https://raw.githubusercontent.com/dwyl/english-words/master/words.txt?raw=true";

    @Override
    public void start(Stage primaryStage) {

        StackPane loadingRoot = new StackPane(new ProgressBar());
        Scene scene = new Scene(loadingRoot, 600, 600);


        List<String> words = new ArrayList<>();

        Task<List<String>> loadWordsTask = new Task<List<String>>() {
            @Override
            public List<String> call() throws Exception {
                try (BufferedReader in = new BufferedReader(new InputStreamReader(new URL(WORD_LIST_URL)
                        .openConnection()
                        .getInputStream()))) {

                    return in.lines()
                            .collect(Collectors.toList());
                }
            }

        };

        ListView<String> suggestions = new ListView<>();

        TextArea textArea = new TextArea();


        textArea.caretPositionProperty().addListener((obs, oldPosition, newPosition) -> {
            String text = textArea.getText().substring(0, newPosition.intValue());
            int index ;

            for (index = text.length() - 1; 
                    index >= 0 && ! Character.isWhitespace(text.charAt(index)); 
                    index--);
            String prefix = text.substring(index+1, text.length());

            for (index = newPosition.intValue(); 
                    index < textArea.getLength() && ! Character.isWhitespace(textArea.getText().charAt(index)); 
                    index++);
            String suffix = textArea.getText().substring(newPosition.intValue(), index);

            // replace regex wildcards (literal ".") with "\.". Looks weird but correct...
            prefix = prefix.replaceAll("\\.", "\\.");
            suffix = suffix.replaceAll("\\.", "\\.");

            Pattern pattern = Pattern.compile(prefix+".*"+suffix, 
                    Pattern.CASE_INSENSITIVE);

            suggestions.getItems().setAll(
                words.stream().filter(word -> pattern.matcher(word).matches())
                .sorted(Comparator.comparing(String::length))
                .limit(100)
                .collect(Collectors.toList())
            );
        });


        BorderPane root = new BorderPane(textArea, null, suggestions, null, null);

        loadWordsTask.setOnSucceeded(e -> {
            words.addAll(loadWordsTask.getValue());
            scene.setRoot(root);
        });

        loadWordsTask.setOnFailed(e -> {
            suggestions.setPlaceholder(new Label("Could not load word list"));
            loadWordsTask.getException().printStackTrace();
            scene.setRoot(root);
        });

        Thread t = new Thread(loadWordsTask);
        t.setDaemon(true);
        t.start();

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}