Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using SwingWorker example

Posted on 2011-03-01
4
Medium Priority
?
1,291 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 750 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 750 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.
Suggested Courses

618 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