The Artima Developer Community
Sponsored Link

Java Answers Forum
problem with classes

1 reply on 1 page. Most recent reply: Jun 3, 2002 10:12 PM by abhijeet

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
geoff

Posts: 3
Nickname: cakewalkr7
Registered: Jun, 2002

problem with classes Posted: Jun 3, 2002 10:59 AM
Reply to this message Reply
Advertisement
I am trying to write a class that creates an object of another class. When I run my code, I expect to see a messagebox when the timer reaches the time passed in to the object, but it doesn't. I thought maybe it was because the program hit the end of the main method, so I threw in the thread to keep it executing, but it still didn't give me the message box. Can anyone tell me what I'm doing wrong? Thanks.

import java.util.*;
import java.text.*;
 
public class EventCalendar
{
	public static void main(String args[])
	{
		Thread t = new Thread();
 
		DateFormat df = new SimpleDateFormat("MM/dd/yy H:mm");
		Date d = new Date();
		try {
			d = df.parse("6/3/02 12:02");
			System.out.println(d.toString());
		}
		catch (ParseException e){
			System.out.println("Unparseable date");
		}
 
		AlarmClock a = new AlarmClock(d);
		try {
			t.sleep(150000);
		} 	
		catch (InterruptedException ie) {}
	}
 
}
 
import java.util.*;
 
public class AlarmClock extends javax.swing.JComponent
{
    	Timer timer;
	TimerTask task = new TimerTask(){
        	public void run()
	{
           javax.swing.JOptionPane.showMessageDialog(null,"You have an event due.");
        	}
	};
 
	public AlarmClock(Date timeToSet){
	
	timer = new Timer(true);
	timer.schedule(task, timeToSet.getTime());
 
	
	}
 
 
}


abhijeet

Posts: 21
Nickname: sony
Registered: May, 2002

Re: problem with classes Posted: Jun 3, 2002 10:12 PM
Reply to this message Reply
Hi geoff,

To execute a thread you have to call the start() method. This will call the method run().

Ex:- Thread newThread = new Thread();
newThread.start();

abhijeet.

Flat View: This topic has 1 reply on 1 page
Topic: swing gui with c++ server Previous Topic   Next Topic Topic: Readfile.java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use