Page 1 of 2 12 LastLast
Results 1 to 20 of 25
Like Tree1Likes

Thread: Why am I getting nothing printed?

  1. #1
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Why am I getting nothing printed?

    I need to write a program that prints the current time every second. Here is what I have and I am not entirely sure why i'm not.


    Java Code:
    import java.util.*;
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import javax.swing.Timer;
    
    public class CurrentTime
    {
       public static void main(String[] args)
       {
          class MyListener implements ActionListener 
          { 
             public void actionPerformed(ActionEvent event) 
             { 
                Date now = new Date(); 
                System.out.println(now);  
             } 
          } 
       MyListener listener = new MyListener(); 
       
       final int refresh = 1000;
       Timer t = new Timer(refresh, listener); 
       t.start();
       
       }
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Why am I getting nothing printed?

    Your JVM exits before it can print anything. After t.start() is executed, your program is done and it stops.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Why am I getting nothing printed?

    So I will have to change the structure of the code or will I have to just move around the code below?

    Java Code:
    final int refresh = 1000;
       Timer t = new Timer(refresh, listener);
       t.start();

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Why am I getting nothing printed?

    Do you HAVE to use the Swing Timer? Is that a requirement? Because the Swing Timer posts events to the EventDispatchThread which you can't control.

    I created this with a Thread object with a while(true) loop and Thread.sleep(1000). This way the thread keeps running so the main thread doesn't stop.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Why am I getting nothing printed?

    That I do have to use. Gotta love college.

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Why am I getting nothing printed?

    Quote Originally Posted by SmokeyWoody View Post
    That I do have to use. Gotta love college.
    Oh God, we're dealing with one of those wonkey professors here right? You know, the one that has Copyright 1999 on his slides...

    Are you SURE you have to use javax.swing.Timer and not java.util.Timer? Just asking...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Why am I getting nothing printed?

    Yes. I get the question from the textbook and the example in the textbook uses java.swing.timer.

    I suppose that I wouldn't HAVE to use it but the question that I am assigned has an example that has to deal with the swing timer. The question all though doesn't specify if a swing timer HAS to be used.

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Why am I getting nothing printed?

    One "hack" you can do is to move the contents of your main() into a SwingUtilities.invokeLater( new Runnable() { ... }). It's a hack, but it works:
    Java Code:
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                 //YOUR CODE HERE
            }
        });
    }
    I have no idea what your textbook/prof is looking for here.

    EDIT: The EDT on which this all relies is used in graphical envorinments. Printing something from main() to System.out is not graphical. Anything you try to do here with Swing/AWT threads is a hax! Let us know what the solution was, because I have a weird feeling about this one.

    When you post the desired solution, I'll post two solutions which I think are better and don't use Swing Timers.
    Last edited by SurfMan; 11-13-2014 at 10:14 AM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Why am I getting nothing printed?

    Can't use things that we haven't learned yet so I am going to skip on the swing timer and just use a regular timer.

  10. #10
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Why am I getting nothing printed?

    I have this now but how do I get it to print? Do I HAVE to use a second class or can it be done within one class?

    Java Code:
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener;  
    import javax.swing.Timer;
    import java.util.*;
    
    
    public class CurrentTime
    {
       class TimerListener implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
             Date now = new Date(); 
             System.out.println(now); 
          }
       }
       public CurrentTime()
       {
          ActionListener listener = new TimerListener();
          final int DELAY = 1000;
          Timer t = new Timer(DELAY, listener);
          t.start();      
       }
    }

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Why am I getting nothing printed?

    This has nothing to do with using one or multiple classes. Same problem as in post #1. You have a Swing Timer. Program ends before Timer can actually do something. You can write this in one single line in one single class and it still wouldn't work.

    This is your code, but in one line. Note that this is exactly the same as your code, and it doesn't work either.
    Java Code:
    public static void main(String[] args) {
        new Timer(1000 ,new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Date now = new Date();
                System.out.println(now);
            }
        }).start();
    }
    Start playing about with java.util.Timer. It can do what you want.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Why am I getting nothing printed?

    Quote Originally Posted by SmokeyWoody View Post
    Yes. I get the question from the textbook and the example in the textbook uses java.swing.timer.
    99.9% sure the book simply has a mistake and it was supposed to be a java.util.Timer. It is pointless to use a Swing timer when you're not doing any Swing GUI programming, as SurfMan has already made quite clear. So the book containing a simple, easy to make mistake is a logical conclusion.

    Nowadays books are pretty much rushed out the door by publishers and no proper reviewing is done first - it is cheaper to setup a webpage for the book and put corrections on there. See if your book has such a website.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why am I getting nothing printed?

    Quote Originally Posted by gimbal2 View Post
    It is pointless to use a Swing timer when you're not doing any Swing GUI programming
    Well, it works. And its easier. Perhaps the professor was introducing them to actionListeners. This is probably the easiest thing to implement to cause events. With a java.util.Timer, you have to use TimerTasks to achieve the same effect.

    But your probably right about the textbook. I wonder in what chapter they talk about using the Swing timer?

    Regards,
    Jim
    Last edited by jim829; 11-13-2014 at 03:34 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Why am I getting nothing printed?

    Quote Originally Posted by jim829 View Post
    Well, it works. And its easier. Perhaps the professor was introducing them to actionListeners. This is probably the easiest thing to implement to cause events. With a java.util.Timer, you have to use TimerTasks to achieve the same effect.

    Regards,
    Jim
    No, it doesn't work, not like this. The only thing to make this work with a Swing Timer is wrap the whole lot in a SwingUtilities.invokeLater(), which makes it even worse. So be it a Timer exercise or an ActionListener exercise, both ways are bad.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Why am I getting nothing printed?

    Another way to get it to execute for a short time is to add this after the call to start():
    Java Code:
       try{Thread.sleep(2000);}catch(Exception x){}; //<<<< let Timer start
    Change the 2000 to let it go longer.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why am I getting nothing printed?

    Quote Originally Posted by SurfMan View Post
    No, it doesn't work, not like this. The only thing to make this work with a Swing Timer is wrap the whole lot in a SwingUtilities.invokeLater(), which makes it even worse. So be it a Timer exercise or an ActionListener exercise, both ways are bad.
    I never said it worked. Of course it doesn't work. Nor did I say that the OP should use the Swing timer in lieu of the GP timer. All the OP needed to do was put a while(true); after the timer is started. The timer does not need to be started on the Event Dispatch Thread (according to the documentation).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Why am I getting nothing printed?

    Quote Originally Posted by jim829 View Post
    I never said it worked. Of course it doesn't work. Nor did I say that the OP should use the Swing timer in lieu of the GP timer. All the OP needed to do was put a while(true); after the timer is started. The timer does not need to be started on the Event Dispatch Thread (according to the documentation).

    Regards,
    Jim
    I must have misunderstood the phrase "Well, it works." A while(true); and a Thread.sleep(n) would work just as well. There's probably another hundred ways to polish this turd :)
    jim829 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why am I getting nothing printed?

    Quote Originally Posted by SurfMan View Post
    I must have misunderstood the phrase "Well, it works."
    Well, you got me there. Clearly it doesn't work. No sure what I was thinking at the time (Oh, I wasn't thinking).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  19. #19
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Why am I getting nothing printed?

    Quote Originally Posted by Norm View Post
    Another way to get it to execute for a short time is to add this after the call to start():
    Java Code:
       try{Thread.sleep(2000);}catch(Exception x){}; //<<<< let Timer start
    Change the 2000 to let it go longer.
    I don't want to drag this any longer than necessary, but when I do that, the program never stops, not even after 2 seconds. I can't seem to find out why though. Curious case... Anyone want to have a crack as to why the program never ends?

    Java Code:
       public static void main(String[] args) {
            new Timer(1000, event -> System.out.println(new Date())).start();
    
            try {
                Thread.sleep(2000);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why am I getting nothing printed?

    I can make a guess. When you start a timer, it is quickly enqueued on a common timer queue inside a thread which is devoted to timers. I doubt the timer thread is a daemon thread so it keeps running, even after the sleep terminates and the main thread ends.

    Edit: Well, I was guessing at something which didn't happen. The program always terminates for me. I should have run it before offering my opinion.

    Regards,
    Jim
    Last edited by jim829; 11-13-2014 at 04:52 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 07-21-2014, 11:26 PM
  2. Output printed in wrong order?
    By SamJava_the_Hut in forum New To Java
    Replies: 3
    Last Post: 08-27-2013, 10:39 PM
  3. Somtimes HTML is printed in my code.
    By Jeremy5 in forum New To Java
    Replies: 1
    Last Post: 11-04-2011, 04:06 PM
  4. JPanel is bigger when printed than on screen
    By mine0926 in forum New To Java
    Replies: 6
    Last Post: 10-08-2011, 09:58 AM
  5. Verify if a file as been, for sure, printed
    By pjmorce in forum Advanced Java
    Replies: 0
    Last Post: 11-20-2008, 10:45 AM

Posting Permissions

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