Article View: comp.lang.java.programmer
Article #275Re: Problems adding a timer
From: John Davison
Date: Fri, 12 Mar 2004 18:51
Date: Fri, 12 Mar 2004 18:51
32 lines
2447 bytes
2447 bytes
On Fri, 12 Mar 2004 11:16:24 +0000, Andy K wrote:
You should use Swing instead of AWT. Not that it really
matters here, but if you're going to be programming GUIs in Java, learn
Swing.
You can use a Thread as a timer. See below.
> public void init() {
// after a bunch of other code
Thead t = new Thread {
public void run() {
while (true) {
// do stuff when timer goes off
stoplight1.doSomething();
stoplight2.doSomething();
// etc...
// then wait for 1 second
try { Thread.sleep(1000); }
catch (InterruptedException e) { }
}
}
};
t.start();
// other init code
} // end public void init()
Note that it's an inner class. You said you're new to Java, so if you
don't understand inner classes yet, go do that before you continue
programming in it.
John Davison
Technical Details
Message-ID:
<pan.2004.03.12.18.51.37.726297@cinci-solutions.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-06!sn-post-01!supernews.com!corp.supernews.com!not-for-mailReferences:
<c2s62r$h2v$2@canard.ulcc.ac.uk>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10fd6d,90c614eee4caf871 X-Google-Attributes: gid10fd6d,public X-Google-Thread: 10a146,90c614eee4caf871 X-Google-Attributes: gid10a146,public X-Google-Thread: 1089d0,90c614eee4caf871 X-Google-Attributes: gid1089d0,public X-Google-Thread: f5533,90c614eee4caf871 X-Google-Attributes: gidf5533,public X-Google-Thread: 115d3b,90c614eee4caf871 X-Google-Attributes: gid115d3b,public X-Google-Thread: f74b3,90c614eee4caf871 X-Google-Attributes: gidf74b3,public X-Google-Thread: 10d861,90c614eee4caf871 X-Google-Attributes: gid10d861,public X-Google-ArrivalTime: 2004-03-12 12:13:32 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-06!sn-post-01!supernews.com!corp.supernews.com!not-for-mail From: John Davison <nospam@cinci-solutions.com> Newsgroups: comp.lang.java.programmer,comp.lang.java.security,alt.www.hotjava,microsoft.public.es.java,microsoft.public.es.java,comp.lang.java.gui,borland.public.jbuilder.java.language,borland.public.jbuilder.java.api Subject: Re: Problems adding a timer Date: Fri, 12 Mar 2004 13:51:41 -0500 Organization: Posted via Supernews, http://www.supernews.com Message-ID: <pan.2004.03.12.18.51.37.726297@cinci-solutions.com> User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.) References: <c2s62r$h2v$2@canard.ulcc.ac.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@supernews.com Lines: 32