Java Swing Refresh

Hey Everyone!

I am creating an Air Traffic Control simulation.  It is going to update an image in the main window every 3 seconds (to account for the radar swinging around - it takes 3 seconds in this simulation).  I have everything accounted for, but I am wondering the best way to do something.  What I need to do is have the system constantly updating the main image (i.e. repainting the screen) every 3 seconds, but also allow the user to be doing something in the meantime (i.e. clicking on stuff, etc...).  I would prefer to stay away from threads, if possible, because that adds an extra layer of complexity that I don't know if I really want to deal with.  Unfortunately, I think that that is perhaps the only way.  

Currently, I have a refresh() method that does everything that needs to be done as far as drawing the screen goes.  I just need it to call the refresh method every 3 seconds, while still allowing the user to interact with the system.

Thanks in advance,

~ace
LVL 1
TopaceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
use in java.util, Timer and TimerTask

U will create a class (like MyTimerTask) that implement TimerTask and have a reference to ur Class instance has the method refresh(), and will call it on (MyTimerTask.run()), then on ur page u will create a timer object and with 5 seconds interval and pass it a reference to MyTimerTask
0
CEHJCommented:
If you need to refresh the gui periodically the proper way to do this is to use javax.swing.Timer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TopaceAuthor Commented:
So I just create a class called MyTimerTask (or equivalent), which extends TimerTask, and calls the refresh() method in its run() method?  I can then schedule it using Timer?  I am assuming that the Timer/TimerTask classes will take care of the threading for me then, or do I have to setup the threads ahead of time?

The problem is that I am not that familiar with Threads in Java, so I'd rather use something that allows me to stay away from the threads aspect of the program (i.e. something that abstracts it away from my control hehe...)

~ace
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

sudhakar_koundinyaCommented:
public void refresh()
{
   int delay = 1000;   // delay for 5 sec.
    int period = 3000;  // repeat every sec.
    Timer timer = new Timer();
   
    timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
              //   your swing tasks here
            }
        }, delay, period);
}

P.S : You need to import java.util.Timer for this
0
CEHJCommented:
>>so I'd rather use something that allows me to stay away from the threads aspect of the program

Using a Swing Timer doesn't use threads explicitly. You need to implement ActionListener, pass that to the Timer ctor and in the event handler (actionPerformed) make the updates to the GUI
0
petmagdyCommented:
>>and calls the refresh() method in its run() method?
yes
>>  can then schedule it using Timer?
yes u add a member method of Timer object in ur refreshed Window class and will create an instance of ur TimerTask has reference to ur window so it can call refresh() , then initialize the Timer member and pass it the TimerTask instance
>>or do I have to setup the threads ahead of time?
no thread involved
Yes, timer and timer task will keep u away of threads,
0
CEHJCommented:
0
CEHJCommented:
The following link explains (especially around the red writing), if you're interested, why updates to a gui should be made from the event dispatch thread. This is what the Swing Timer is designed for, and why it is distinct from the java.util.Timer

http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
0
petmagdyCommented:
Topace..

Take a look at CEHJ sample, seems better solution
0
TopaceAuthor Commented:
Actually, the reason I am doing all of this is going to be for animation basically.  It's going to be animating a series of icons, displaying the planes' movement across the screen.  As such, I am going to take a look at the suggestions offered, and see if I can get them to work for my purpose.  I anticipate that I will respond again and award points tomorrow afternoon.  

~ace
0
TopaceAuthor Commented:
Thanks everyone for helping out.  I was able to get the javax.swing.Timer to work correctly.

~ace
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.