Results 1 to 6 of 6

Thread: [newibe] fix?

  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [newibe] fix?

    See anything you can fix here?

    Java Code:
    package homenetwork.bkr.training;
    
    import java.awt.event.ActionListener;
    
    import javax.swing.Timer;
    import javax.swing.JOptionPane;
    
    public class TimerTest {
    	public static void main (String[] args)
    	{
    		TimePrinter listener = new TimePrinter();
    		
    		//timer that calls the listener every 10 seconds
    		Timer t = new Timer(10000, listener); 
    		t.start();
    		
    		JOptionPane.showMessageDialog(null, "Quit program?");
    		System.exit(0);
    		
    	}
    
    }
    ...
    
    package homenetwork.bkr.training;
    
    import java.awt.*; //I wrote this import
    import java.awt.event.ActionEvent; //why did I have to write this?
    import java.util.*;
    
    public class TimePrinter {
    	public static void actionPerformed (ActionEvent event)
    	{
    		Date now = new Date();
    		System.out.println("At the tone, the time is: " + now);
    		Toolkit.getDefaultToolkit().beep();
    	}
    }
    
    Error: Exception in thread "main" java.lang.ClassCastException: homenetwork.bkr.training.TimePrinter cannot be cast to java.awt.event.ActionListener
    	at homenetwork.bkr.training.TimerTest.main(TimerTest.java:14)
    :confused:

  2. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Your PrintWriter class is not of type ActionListener. The Timer class takes an int and an ActionListener for it's parameters. You have successfully implemented the methods in the ActionListener interface, however, you haven't told the compiler so.

    Java Code:
    public class TimePrinter implements ActionListener
    Should do the trick

    Mr. Beans

    Also next time please try to use a more discriptive title. "Casting Exception" or something of the sort. Not "Fix!"
    Last edited by Mr.Beans; 05-20-2009 at 05:34 PM.

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Thanks, was in a hurry sorry :)
    Compiles but doesn't wait for 10 seconds for the JOptionPane.showMessageDialog to show up.

    :confused:

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    No, it wouldn't. Execution of the current thread doesn't wait after Timer.start(). If you want a delay before the message appears you need to attach it to another timer.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Would you explain in code syntax (even rough) pls?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    put your query at the end of the code block in your current timer's actionperformed method. You'll want to use a different JOptionPane method than the showMessageDialog, one that allows the user to respond. Please read the JOptionPane API, and you'll find the one to use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •