Results 1 to 3 of 3
- 03-04-2009, 04:11 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
Using ActionListener with a Timer Object
Hellooo
Can someone please have a look at this code and telling me what I'm doing wrong. I'm trying to get a message printed every two seconds but whenever I run the program, nothing is printed.
Java Code:import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; import java.util.GregorianCalendar; import javax.swing.Timer; public class Testing { private int id; public Testing(int id) { this.id = id; } public void setTime(GregorianCalendar appt) { // GregorianCalendar now = new GregorianCalendar(); // this.appt = appt; //(int)appt.getTimeInMillis()-(int)now.getTimeInMillis()+10 Timer t = new Timer (2000, new ActionListener () { public void actionPerformed (ActionEvent e) { System.out.println("You have an appointment in 10 minutess."); } }); t.start(); } public boolean equals(int otherId) { if(this.id == otherId) return true; else return false; } }
Java Code:import java.util.GregorianCalendar; public class Tester { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub Testing appt = new Testing(1); GregorianCalendar date = new GregorianCalendar(2009, 3, 4, 13,45); appt.setTime(date); } }
I'm supposed to use the date as an argument, but I'm using 2000ms just for now to test it, so that's why the GregorianCalendar is commented.
Thx
- 03-04-2009, 05:42 PM #2
javax.swing.Tmer is designed to run the listening method on the Event Dispatcher Thread, which is where all work for Swing components takes place. Since you don't have any active Swing components, I don't know what the Timer would do, quite possibly nothing. Is that what you are seeing?
Create a simply Swing UI and have the Timer ActionPerformed update the UI.
-
Similar Threads
-
Concurrent timers are not getting invoked for same timer info (Serializable object co
By Neeraj in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-31-2008, 02:20 PM -
Demonstrating the ActionListener
By Java Tip in forum java.awtReplies: 0Last Post: 04-23-2008, 08:20 PM -
How to use KeyListener and ActionListener
By Java Tip in forum javax.swingReplies: 0Last Post: 04-23-2008, 08:19 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM -
ActionListener interface
By tsantana in forum New To JavaReplies: 2Last Post: 03-30-2008, 10:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks