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

  • SEARCH
  • Home
  • 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 8048247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:22:22+00:00 2026-06-05T06:22:22+00:00

I have a button click event that will fire a swing worker thread which

  • 0

I have a button click event that will fire a swing worker thread which in return fire another thread to do a long calculation including writing a file. Then this file is read to draw some graphics. However drawing part never happens if i don’t add a delay in between.. (It says file not found although the file is there..What is the better way to fix this without adding a delay..

 private void buttonFragmentActionPerformed(java.awt.event.ActionEvent evt) {                                               
    try
    {
        ESIPlusFragmenterWorker epfw = new ESIPlusFragmenterWorker(10, sdfFile, cidSpectrum);
        epfw.execute();

        Thread.sleep(1000);

        holder.molTable1.drawMolViewPanel(currDir+sep+"esiFragments"+sep+"esiFrag.sdf");
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Swing Worker

public class ESIPlusFragmenterWorker extends SwingWorker<Void, Void>{

int mzppm_;
String SDF_;
String spectrum_;
Double mion_;
MolTable holder_;

ESIPlusFragmenterWorker(int mzppm, String SDF, String spectrum)
{
    mzppm_ = mzppm;
    SDF_ = SDF;
    spectrum_ = spectrum;
}

@Override
protected Void doInBackground() {
    try
    {
    Molecule mol;
    MolImporter importer = new MolImporter(SDF_);
    ExecutorService te = Executors.newFixedThreadPool(1);
    while ((mol  = importer.read()) != null) 
    {
     Runnable epf = new ESIPlusFragmenter(mol, spectrum_, mzppm_);
     Thread t = new Thread(epf);
     te.execute(epf);
    }
    importer.close();
    te.awaitTermination(10, TimeUnit.MINUTES);
    }
    catch (Exception e)
    {
      //  
    }
    finally
    {
        return null;
    }
}


@Override
protected void done() {
    try {
        //
    } catch (Exception e) {
        //e.printStackTrace();
    }
}

}

  • 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-05T06:22:23+00:00Added an answer on June 5, 2026 at 6:22 am

    Never, never, never call Thread.sleep(...) on the EDT as this will put your entire GUI to sleep. And besides, what if you estimate wrong, and the background process takes longer than your sleep delay time?

    One possible solution is to add a PropertyChangeListener to the SwingWorker and listen on the “state” property for the SwingWorker.StateValue to be SwingWorker.StateValue.DONE, then do your drawing.

    e.g.

    private void buttonFragmentActionPerformed(java.awt.event.ActionEvent evt) {
      try {
         ESIPlusFragmenterWorker epfw = new ESIPlusFragmenterWorker(10,
               sdfFile, cidSpectrum);
         epfw.addPropertyChangeListener(new PropertyChangeListener() {
    
            @Override
            public void propertyChange(PropertyChangeEvent pcEvt) {
               if ("state".equals(pcEvt.getPropertyName())) {
                  if (pcEvt.getNewValue().equals(SwingWorker.StateValue.DONE)) {
                     holder.molTable1.drawMolViewPanel(currDir + sep
                           + "esiFragments" + sep + "esiFrag.sdf");
                  }
               }
            }
         });
         epfw.execute();
    

    So what this does is waits until the SwingWorker has completed its business before calling the code inside of the listener.

    Another option is to call your holder.molTable1.drawMolViewPanel inside of the SwingWorker’s done() method, and this will work too, but by doing it as noted above with a PropertyChangeListener, the SwingWorker doesn’t have to have any knowledge about the code called in the listener (as opposed to using SwingWorker’s done() method), and this may allow for looser coupling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a local thread that is started on button-click event from the main
I have a button click event that takes information from controls and enters it
I have button i want to disable the click event on it when app
I have MainWindow with a button, under the button click event I want MainWindow
I have a fairly simple code, a button click event, with the first line
I have the following code bound to the click event of a button: function
Does anyone have a technique that will allow me to determine which element has
I know that using .submit will trigger an event when the submit button is
I have declared variable of TStringList in private section. In a button click event
I have a aspx page which has three tabs. When I click a button

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.