Solved

Using SwingWorker example

Posted on 2011-03-01
4
1,277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

705 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