You probably don't actually want to play a sound whenever the IDE detects an error - different parts of the IDE may request a reparse of the currently edited file (or any other file) for any reason at any time, including a single edit triggering several consecutive reparses, depending on the order things happen in.
You also likely don't want to play a sound every time an error is encountered ANYWHERE in the currently edited file, as it's easy to, say, change the signature of a method at the bottom of a file, turning calls to that method into errors. You *also* probably don't want to play a sound on every character of an incompletely typed keyword until it is complete. There is one thing I can't stress enough: *In an IDE, the normal state of source files is broken.* What I *think *you actually want to do is: * Listen for changes in which document is currently being edited (listen for changes on TopComponent.getRegistry(), try to get an EditorCookie from the currently active component, if it has a document, get it and listen on it; or EditorRegistry may offer a simpler path to get the current editor) * Listen for changes in that document (plain old Swing document listener, but probably ask - I think it is one of NbEditorUtilities, LineDocumentUtilities or NbDocument - has a method isTypingModification(DocumentEvent) that will let you differentiate automated document changes like pastes and refactorings, from actual user typing * Listen for changes in the caret position in that document - from the document, get the editor from EditorRegistry, then JTextComponent.getCaret().addCaretListener() * Listen for updates to the *error annotations* on that document. I don't know a fabulous, supported way to get that - the one module I ever wrote that needed to do that achieved it via the hack of replacing the default editor AnnotationViewFactory (thing that supplies the component with the marks in the right margin of the editor on syntax errors, diffs, etc.) with my own clone of the default one, which I could then get the error annotations from: https://github.com/timboudreau/netbeans-contrib/blob/master/fisheye/src/main/java/org/netbeans/modules/fisheye/hacks/FisheyeAnnotationViewFactory.java * Make your sound *only when:* * The user has stopped typing for some threshold number of milliseconds (350 is probably a good starting point) - RequestProcessor.Task is your friend for rescheduling the pending potential beep on keystrokes * An error annotation exists or comes into existence shortly after typing stops *within one character of the caret position* Now, all that said, I have to ask: Is what you're building a torture device? What is the actual problem you're trying to solve? -Tim On Thu, Oct 15, 2020 at 8:35 AM <[email protected]> wrote: > Greetings: > > > > I hope that you and all your loved ones are safe and healthy. I write to > you because I have spent days scouring over the documentation trying to > find > out how could I create a Netbeans module that plays a feedback sound > whenever the user makes a syntax mistake when typing (Java) code. All > documentation I have read requires me to create my own Java lexer and > parser, which I think can be avoided if I could just piggyback on the > built-in Java parser and install a listener or something somewhere (I don't > know!) that allows me to playback the sound when the error is detected. > Please help! This is for my doctoral dissertation. > > > > Best regards, > > Henry F. Bruckman Vargas > > > > -- http://timboudreau.com
