Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8555799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:12:24+00:00 2026-06-11T15:12:24+00:00

In the following lines of code when Button1 is pressed the Jframe becomes unresponsive

  • 0

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??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:12:25+00:00Added an answer on June 11, 2026 at 3:12 pm

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Encounter following lines of code, but couldn't understand it. What is this (/ ...
In the following lines of code, I need to adjust the pointer pm by
I have the following lines of code: if(std::binary_search(face_verts.begin(), face_verts.end(), left_right_vert[0]) && std::binary_search(face_verts.begin(), face_verts.end(), left_right_vert[1]))
Take the following lines of code that would work fine in a c# asp.net
Given the following lines of code which is using JQTOUCH: $('#customers').bind('pageAnimationEnd', function(e, info){ if
I am using the following lines of code to download and save an html
I'm using the following lines of code in my .htaccess file to create redirects.
So I have the following lines of code in a function sock = urllib.urlopen(url)
I have been wondering about the following lines of code [self performSelector:@selector(myMethod) withObject:self afterDelay:1.0];
I have been trying to find out why the following lines of code do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.