• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2168
  • Last Modified:

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
0
Topace
Asked:
Topace
  • 5
  • 3
  • 3
  • +2
1 Solution
 
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
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now