I am creating a java program that highlights orthographic errors based on a previously loaded dictionary. What I'm trying to do is to highlight the errors as the user is writing in a JTextArea, my problem is that when you finish writing the last word and press the space or enter an event that analyzes the last word is triggered , which was obtained through a DocumentListener, and if it is not in the dictionary it highlights it but then if I continue writing without correcting the error, everything I write after that word is highlighted, and I just want it to highlight the word if it is wrong and then continue normal without highlighting and if it finds another word with an error that highlights it. To get the start and end of the last word I use the Utilities class in the DocumentEvent.
private class EventDocument implements DocumentListener, KeyListener { private int start; private int end; Private String word;
@Override
public void changedUpdate(DocumentEvent arg0) {
chequearPalabra();
}
@Override
public void insertUpdate(DocumentEvent arg0) {
chequearPalabra();
}
@Override
public void removeUpdate(DocumentEvent arg0) {
}
private void chequearPalabra(){
try {
start = Utilities.getWordStart(texto, texto.getCaretPosition());
end = Utilities.getWordEnd(texto, texto.getCaretPosition());
palabra = texto.getDocument().getText(start, end-start);
errores.setText(palabra);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_SPACE){
if(!Revisor.getTrie().buscar(palabra.toUpperCase())){
System.out.println(Revisor.getTrie().buscar(palabra.toUpperCase()));
resaltarPalabra(start, start+palabra.length());
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}