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 7888339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:48:26+00:00 2026-06-03T05:48:26+00:00

How do you get the SwingWorker that code is currently running in? You can

  • 0

How do you get the SwingWorker that code is currently running in? You can use Thread.currentThread() to get the Thread instance, but I need the SwingWorker instance.

Code from the comments

private static void loadFeaturesForSym(final SeqSymmetry optimized_sym, 
                                       final GenericFeature feature) 
  throws OutOfMemoryError { 
  final CThreadWorker<Boolean, Object> worker = 
     new CThreadWorker<Boolean, Object>("Loading feature " + feature.featureName) { 
         @Override 
         protected Boolean runInBackground() { 
           try { 
             return loadFeaturesForSym(feature, optimized_sym); 
           } catch (Exception ex) { 
             ex.printStackTrace(); 
           } 
           return false; 
         } 
       }; 
       ThreadHandler.getThreadHandler().execute(feature, worker); 
     } 
   }
  • 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-03T05:48:27+00:00Added an answer on June 3, 2026 at 5:48 am

    I recommend you create a model object to which the SwingWorker can listen and send those updates out the publish and process methods. Your other objects should not know about SwingWorker, they should just know about their own progress and publish that out to whoever wants to listen. It’s called decoupling. Here’s one idea for doing it, which uses something that approaches MVC. I have not compiled this code, but it helps explain what I am talking about.

    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeSupport;
    
    public class ProcessStatus {
        public static final String PROGRESS = "Progress";
    
        private PropertyChangeSupport propertyChangeSupport;
    
        private int progress = 0;
    
        public void addPropertyChangeListener(PropertyChangeListener listener) {
            propertyChangeSupport.addPropertyChangeListener(listener);
        }
    
        public void removePropertyChangeListener(PropertyChangeListener listener) {
            propertyChangeSupport.removePropertyChangeListener(listener);
        }
    
        protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
            propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
        }
    
        public void setProgress(int progress) {
            int oldProgress = progress;
            this.progress = progress;
            firePropertyChange(PROGRESS, oldProgress, progress);
        }
    
        public int getProgress() {
            return progress;
        }
    }
    
    public class SomeWorker extends SwingWorker implements PropertyChangeListener {
        public void doInBackground() {
            ProcessStatus status = new ProcessStatus();
            status.addPropertyChangeListener(this);
            ProcessorThingy processor = new ProcessorThingy(status);
            processor.doStuff();
        }
    
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(ProcessStatus.PROGRESS)) {
                publish((Integer) evt.getNewValue());
            }
        }
    }
    
    public class ProcessorThingy {
        private ProcessStatus status;
    
        public ProcessorThingy(ProcessStatus status) {
            this.status = status;
        }
    
        public void doStuff() {
            //stuff part 1
            status.setProgress(10);
            //stuff part 2
            status.setProgress(50);
            //stuff part 3
            status.setProgress(100);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Get this simple python code, same matching with re.compile instance. I noticed that even
.get() converts a jQuery object to a DOM element that Javascript can use without
i get a valid fd object from a caller. How can i find out
I'm trying to get a SwingWorker to work. I've the following code at the
I get this 'error' when running PEVerify on a custom generated assembly. [MD](0x8013124C): Error:
I get the memberOf property for my user using this code: DirectorySearcher search =
I have a SwingWorker thread with an IOBound task which is totally locking up
Consider this code: public void actionPerformed(ActionEvent e) { setEnabled(false); new SwingWorker<File, Void>() { private
I've written an application in Swing that listens for UDP packets from a smart
I'm trying to execute a SwingWorker (SubWorker) from another SwingWorker (MainWorker), and then I

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.