Hello. Faced such a problem, when I initialize an application, a DocumentListener is created, which is responsible for highlighting syntax, but after I load another document with:

private void readInFile(String fileName) { try { FileReader r = new FileReader(fileName); textArea.read(r,null); r.close(); currentFile = fileName; frame.setTitle(currentFile); changed = false; } catch(IOException e) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(textArea, "Editor can't find the file called "+fileName); } } 

The backlight stops working. I tried to make the creation in a separate method:

  private void setDocumentListener() { textArea.getDocument().addDocumentListener(new DocumentListener() { //Здесь код } } 

I registered the call to the internalization and to the readInFile method so that it was called again when the new document was loaded, but this did not work. It also works only until the first download of the new document. Tell me what could be the reason and what am I doing wrong?

  • and how and when do you call setDocumentListener ? - Mikhail Vaysman

1 answer 1

Thanks to Mikhail Vaysman for the comment, this question led me to the answer. I called setDocumentListener () in the readInFile () method itself and it did not work, and after adding it to the method where readInFile () was called, everything worked fine. The question is closed.