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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:56:18+00:00 2026-05-27T22:56:18+00:00

I have create a GUI based app using java based on MVC . the

  • 0

I have create a GUI based app using java based on MVC . the major task of my app is to get some data from internet and starts to parsing it.

for getting data from internet I use following class :

public class WebReader implements Runnable {
    WebRecordResult WRResult = new WebRecordResult ();
    private void    getData(args){
        .
        .
        .
        setResult(result);
    }

    public void run() {
        getData(args);
        WRResult.setResult(getResult());
    }

}


public class WebReaderResult {
    private AtomicReference<String> result = new AtomicReference<>("");

    public String getResult() {
        return result.get();
    }

    public void setResult(String s) {
        result.set(s);
    }
}

and inside my controller I have created something like :

WebReaderResult wrr = new WebReaderResult();

ExecutorService executor = Executors.newSingleThreadExecutor();
WebReader wr =new WebReader(args);
Future<?> f1 = executor.submit(wr);
executor.shutdown();
try {
    f1.get();
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}
wrr.getResult(); 

still after using this code my GUI gets freezed until data becomes available. I don’t want my GUI to be freezed. what shoud I do?

Note : my GUI is swing

  • 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-05-27T22:56:18+00:00Added an answer on May 27, 2026 at 10:56 pm

    The line

    f1.get();
    

    blocks until your thread is done executing. If you execute this line in your UI thread, the UI will freeze, of course. But what are you doing with the result anyway? Without more concrete code, it is hard to give a proper solution to your problem.

    ** EDIT **

    Your controller should execute and handle tasks in it’s own thread, and should be completely separated from the UI (for a proper MVC model). Since there are missing information in your code (args, etc.), an example could be :

    Results

    public class WebReaderResult<V> {
       private AtomicReference<V> ref = new AtomicReference<V>();
    
       public V getResult() { return ref.get(); }
       public void setResult(V value) { ref.set(value); }
    }
    

    Task

    public abstract class WebTask<V> {
       private WebReaderResult<V> res = new WebReaderResult<V>();
    
       public WebReaderResult<V> getWebReaderResult() { return res; }
    
       // handle task here
       public abstract void handle();
    }
    

    Controller

    public interface WebControllerCallback<V> {
       public void done(V result);
    }
    
    public class WebController {
       static private WebController instance;
       static public WebController getInstance() {
          if (null == instance) instance = new WebController();
          return instance;
       }
    
       private ExecutorService executor = Executors.newSingleThreadExecutor();
    
       public void handleTask(WebTask<?> t, WebControllerCallback<?> cb) {
          executor.submit(new Runnable() {
             public void run() {
                t.handle();
                cb.done(t.getWebReaderResult());
             }
          });
       }
    
       // other methods here
    }
    

    UI Code

    (in your ActionListener, or any other UI event method)

    WebTask<String> task = new WebTask<String>() {
       public void handle() {
          WebReaderResult<String> result = getWebReaderResult();
    
          // TODO : handle task and set result here result.getResult();
       }
    };
    WebControllerCallback<String> callback = new WebControllerCallback<String>() {
       public void done(String result) {
          // TODO : update UI here from result value
       }
    };
    
    WebController.getInstance().handleTask(task, callback);
    

    ** EDIT 2 **

    As mentioned by other users, since Java 1.6 there is a class called SwingWorker that you may use to do exactly that, but with a lot less code. Just put something like this wherever required in your UI event (or create a separate class to implement the methods) :

    new SwingWorker<String, Object>() {
       @Override
       protected String doInBackground() throws Exception {
          // TODO : process result
    
          return "some result";
       }
    
       protected void done() {
          // TODO : refresh UI with the value of this.get(); which return a String
       }
    }.execute();
    

    This last edit is not as clear as the first example, however the SwingWorker class does offer what you are looking for. Now, all you need is to delegate the processing done in both doInBackground and done using your WebReader class (ie. by calling wr.run(); in the doInBackground, or something.). In any case, I believe you have plenty to go on and about with this now.

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

Sidebar

Related Questions

I have an object guiObject that comes from GUI. Based on its data fields
I have an assignment to create a GUI using MATLAB GUIDE and am having
I am using swing to create my GUI. J have a JFrame containing one
In my Java course I have to create a GUI class that interacts with
I currently have a class and I'm trying to create an easy GUI to
I have created a fairly substantial Java GUI application with many form windows where
I have create my own NSOpenGLView class, right now the data that i want
I have an existing app with a command-line interface that I'm adding a GUI
I'm trying to port a GTK-based Linux app to Mac OS-X. I have the
I want to create a very simple HTML/AJAX based GUI for a Python program.

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.