Solved

Using SwingWorker example

Posted on 2011-03-01
4
1,266 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 298
Better way to make a string with template variables in java 3 34
CSV file parsing thru Java 13 39
Java array 10 63
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

733 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