Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Question: How to add timers in GUI via netbeans

Status
Not open for further replies.

JTan

Technical User
Oct 9, 2001
73
SG
Hi all,

I am creating my GUIs via netbeans.

I would like to add a timer in the interfaces so that I can link from a page to another. Is there any fields in the properties tab which I can just fill in to create the timer? Or must I add codes in it? If so, may I know how to create the timer?

I am sorry for asking so many questions. I am totally new to netbeans and is not very familar GUIs via Java.swing.

Thanks in advance for any information given!
 
I think the way in which i would solve this problem is to have a Thread and an array which holds some kind of pair of long -> JPanels. The thread essentially checks the longs in the array to check if a given time has passed, and swaps over the associated Panel with the current one if it has.

Here's a bit of code which should work - it's untested but does compile... the only thing is that the Slice[] you pass to the constructor should be ordered by the time... you may want to add implement Comparable in the Slice class to do some easy sorting.

And finally, I'm not sure if this is the best way to exit a Thread (if (!exit)), but i've used it for a while and never found it to be a problem - if anyone could enlighten me on that one, that'd be great.

Code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Date;


public class PanelSwitcher extends Thread {
    private Slice[] frames;
    private JFrame window;
    private boolean exit = false;

    public PanelSwitcher(Slice[] frames, JFrame window) {
        this.frames = frames;
    }

    public void run() {
        long startTime = (new Date()).getTime();
        int index = 0;

        while (index < frames.length && !exit) {
            long now = (new Date()).getTime();
            if ((now - startTime) > frames[index].getTime()) {
                window.setContentPane(frames[index++].getPanel());
            }
            //sleep for 1/5 a second ~ or your minimum step interval
            try {
                sleep(500);
            } catch (InterruptedException ie) {
                //... not much i can do here i don't think...
            }
        }
    }

    public void exit() {
        this.exit = true;
    }

    public class Slice {
        private long time;
        private JPanel panel;

        public Slice(long time, JPanel panel) {
            this.time = time;
            this.panel = panel;
        }

        public long getTime() {
            return this.time;
        }

        public JPanel getPanel() {
            return this.panel;
        }
    }
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top