calling a time consuming method from a JButtonActionPerformed

In the following lines of code when Button1 is pressed the Jframe becomes unresponsive till the execution of the encode() method is completed. But I need to update a progress bar in the Jframe displaying the progress.

private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    try {
        if (flagState == 0) {
            WavFile.decode(readWavFile, msg.getText(), key.getText().hashCode());
        } else if (flagState == 1) {
            WavFile.encode(readWavFile, msg.getText(), key.getText().hashCode());            
        }
    } catch (WavFileException | IOException e) {
        notes.setText(e.getMessage());
    }
}         

I thought of doing this

private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    try {
        if (flagState == 0) {
            Thread t = new Thread(new Runnable() {
                               public void run() {
                                  WavFile.decode(readWavFile, msg.getText(), key.getText().hashCode()); 
                               }
                            };)
            t.start();
        } else if (flagState == 1) {
            Thread t = new Thread(new Runnable() {
                               public void run() {
                                  WavFile.encode(readWavFile, msg.getText(), key.getText().hashCode()); 
                               }
                            };)
            t.start();
        }
    } catch (WavFileException | IOException e) {
        notes.setText(e.getMessage());
    }
} 

and update the progress bar from the encode() method.

But I understand that it will a strong coupling between the modules and i dont want that.

I also dont think that calling a new thread every time is a good idea ( thought of ExecutorService but not sure how to use it).

What should I do to make this into a decent piece code??

Answers


Threading is definitely something you have to introduce here. Anything that takes a significant amount of time to complete should not be run in the awt event thread.

Spawning a new thread each time the button is clicked is not inherently a problem. You should consider, though, that it may be possible to spawn many threads doing the same thing before even the first completes. You could use an Executor if you want, but it probably is not necessary.

As for tracking progress, I would add another thread that periodically queries the other thread and updates the progress bar as necessary. Here's how I would structure it:

private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {
    disableButton1();
    final Thread t;
    if (flagState == 0) {
        t = new Thread(new Runnable() {
            public void run() {
                try {
                    WavFile.decode(readWavFile, msg.getText(), key.getText().hashCode());
                } catch (WavFileException | IOException e) {
                    notes.setText(e.getMessage());
                }
            }
        });
    } else if (flagState == 1) {
        t = new Thread(new Runnable() {
            public void run() {
                try {
                    WavFile.encode(readWavFile, msg.getText(), key.getText().hashCode()); 
                } catch (WavFileException | IOException e) {
                    notes.setText(e.getMessage());
                }
            }
        });
    }

    Thread monitor = new Thread(new Runnable() {
        public void run() {
            try {
                while (notComplete(t)) {
                    setProgressBar(getProgress(t));
                    Thread.sleep(SLEEP_TIME);
                }
                t.join();
            } finally {
                enableButton1();
            }
        }
    });

    t.start();
    monitor.start();
}

n the following lines of code when Button1 is pressed the Jframe becomes unresponsive till the execution of the encode() method is completed. But I need to update a progress bar in the Jframe displaying the progress.

you have issue with Concurency in Swing, you block the EDT, caused unresponsive GUI, until code ended


Need Your Help

Subscripting a pointers interchangeably with a 2D array in Objective-C?

objective-c pointers casting static-array

I have a statically declared array like this Cell SMatrix_A[36][10]. When I use SMatrix_A directly in my code (Cell foo = SMatrix_A[12][8]), everything works fine. However, what I really want is to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.