Solved

Using SwingWorker example

Posted on 2011-03-01
4
1,256 Views
Last Modified: 2012-05-11
I am new to java and swing and I'm having a hard time working with swingworker.  My goal is to have a interface that allows the user to click a button which will download data from a php page on my web server.  It will give a json string as a response.  This program will update the embedded database in the program syncing the 2 databases.  This si what I thought the update class should do:

1) layout the GUI for the user - it will have an download button (have this done)
2) have a progress bar so the user knows it is moving along (not done)
3) have an actionlistener for the download button (done)
4) create a swingworker thread to connect to the url and get the json string (not done)
5) parse the json into a java object(not done)
6) update the database tables (around 10 in total), pass back progress to progress bar after each update(I can do this)
7) have a popup notify the user when the background update is done

I am using Gson as my json parser.

I will attach the class file I have right now.  I don't need the code to be tested but just a general idea of the best way to go about it.  I am just having a hard time wrapping my head around the process of it all.  For the database stuff it will be enough to say "update database here".  Really just need how to setup the swingworker thread.

Thanks Nick Update.java
0
Comment
Question by:ninedoors19
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 35010999
Personally, i always implement the SwingWorker as a named inner class. In your case that would be something like the following. Prefer the faster HashMap over Hashtable unless you need synchronization


private class DataFetcher extends SwingWorker<Void, Map<String, String>> {
        public Void doInBackground() {
	    // Get the data
        }
    }

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 35012490
consider breaking it up a little, for example your code to get the json string can be in a separate class.
the code to handle update the database should also be seperated out, mixing backend code with your gui just ends up being a mess.

You're code will be a lot cleaner and easier to maintain that way.
0
 

Author Closing Comment

by:ninedoors19
ID: 35475616
Was looking for a more in depth answer.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35476325
:)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question