Question

i have a voice recognition system, this is the listen(button) function

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       

    // TODO add your handling code here:

    jButton1.setIcon( loading_icon );
    microphone.startRecording()
    jButton1.setIcon( speak_icon );

    System.out.println("Start speaking. Press Ctrl-C to quit.\n");

    result = recognizer.recognize();
    if (result != null) {
         String bestfinalnofiller = result.getBestFinalResultNoFiller();
         String hypothesis = result.getBestPronunciationResult();
         String getresult = result.getBestResultNoFiller();

         System.out.println("You said: " + bestfinalnofiller + '\n');
         System.out.println("You said: " + hypothesis + '\n');
         System.out.println("You said: " + getresult + '\n');

    } else {

    } 
}      

my result will be like: the button icon stays the same and changes after the work is done the system prints in while the work is going string "start speaking..."

recognizer.recognize() function:

public Result recognize(String referenceText) throws IllegalStateException {

    Result result = null;
    checkState(State.READY);
    try {
        setState(State.RECOGNIZING);
        result = decoder.decode(referenceText);
    } finally {
        setState(State.READY);
    }
    return result;

}

I tried to add:

SwingUtilities.invokeLater

but didn't work, please help me, I want to make the button changes it's icon image while the calculations ...

Was it helpful?

Solution

It is likely that recognizer.recognize() is a blocking method, that is, until it is finished, it will not return.

What this means is until the method returns, you are blocking the Event Dispatching Thread, which is responsible for, amongst other things, process paint requests.

This means until your method returns, nothing can be painted or updated (or responded to).

A simple solution would be to use a SwinWorker, which would allow you to set up a background process in order to perform the blocking call but also provides a number of methods synchronise any updates to the UI via the EDT

Take a closer look at Concurrency in Swing and SwimgWorker for more details

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top