Я пытаюсь реализовать функцию автозаполнения в текстовом редакторе, который я разработал на Java. Чтобы реализовать автозаполнение, мне нужны все (отличные) слова, которые уже были введены в редактор.Tokenizing Text в JTextComponent на каждом неабсолютно-цифровом символе
Прямой реализацией будет преобразование JTextComponent в строку, а затем токенизацию. Вместо этого есть ли способ запомнить, где был введен последний неадминистративный символ, начните запись строки до тех пор, пока не будет напечатан другой буквенно-цифровой символ, а затем добавьте эту записанную строку в набор слов в моем автозаполненном словаре?
/*Contains only parts of code that is relavant to the question*/
public class Editor {
private JFrame editorFrame;
/*this is where text typed is shown*/
private JTextComponent textComp;
/*autoComplete contains all words in the entered text*/
/*private TreeSet<String> autoComplete*/
/*private JMenu autoCompleteMenu*/
public static void main(String[] args) {
Editor editor = new Editor();
editor.Launch();
}
private void Launch() {
editorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editorFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
editorFrame.setVisible(true);
editorFrame.pack();
}
public Editor() {
editorFrame = new JFrame("Critter");
JTextArea ta = new JTextArea("", 46, 115);
ta.setLineWrap(true);
textComp = (JTextComponent)ta;
}
/*
Iam trying to implement the following
*/
/*
specialChars = {all special characters,space,tab}
if typed character is in specialChars:
while KeyEvent is alpha-numeric :
record KeyEvent to a string
search for this recorded string in autoComplete and create autoCompleteMenu of possible completions
if autoCompleteMenu != null:
display autoCompleteMenu
add string to autoComplete
*/
/*I want to know how to detect the keyevent belonging to specialChars and recording the string */
}
Пожалуйста, разместить код примера –
я добавил соответствующие части исходного кода. – reek