Using SwingWorker to run background process


The following example fetches Facebook data in a background process and passes the data to the GUI.

SwingWorker<User, String> fetchUserWorker = new SwingWorker<User, String>()
{
@Override
protected User doInBackground() throws Exception
{
IFacebookController controller = new RestFBController(accessToken);

User user = controller.fetchMe();

return user;
}

@Override
protected void done()
{
User user;
try
{
BackgroundProcess.this.mainGui.appendProgressLog("Fetched user record");

// get the user object from doInBackground()
user = get();
mainGui.setCurrentUser(user);
}
catch (InterruptedException | ExecutionException e)
{
BackgroundProcess.this.mainGui.appendProgressLog("ERROR fetching facebook data "+e.getMessage());
e.printStackTrace();
}

}
};

BackgroundProcess.this.mainGui.appendProgressLog("Fetch Current User - Connecting to Facebook");

fetchUserWorker.execute();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Up ↑

%d bloggers like this: