Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Timer class countdown

  1. #1
    Junior Member p0oint's Avatar
    Join Date
    Jun 2010
    Location
    Macedonia
    Posts
    10
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Timer class countdown

    Hello!

    I got the following code:

    import javax.swing.JFrame;
    import javax.swing.Timer;
     
    class MainClass extends JFrame {
      Timer timer;
     
      int counter;
     
      MainClass(String title) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        ActionListener a = new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            System.out.println("Counter = " + counter);
     
            if (++counter > 10) {
              timer.stop();
              System.exit(0);
            }
          }
        };
     
        timer = new Timer(300, a);
        timer.start();
     
        pack();
        setVisible(true);
      }
     
      public static void main(String[] args) {
        new MainClass("Timer Demo1");
      }
    }

    Now I got question. ActionListener is actually a interface. How is allowed to create instance of the interface ActionListener by stating ActionListener a = new ActionListener() ?

    I got another question about the red part:

        ActionListener a = new ActionListener() [COLOR="Red"]{
          public void actionPerformed(ActionEvent e) {
            System.out.println("Counter = " + counter);
     
            if (++counter > 10) {
              timer.stop();
              System.exit(0);
            }
          }
        };[/COLOR]

    What is actually the red part?

    Thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Timer class countdown

    The red part is the body of the class 'a' that implements the ActionListener interface.

  3. The Following User Says Thank You to Norm For This Useful Post:

    p0oint (August 25th, 2010)

  4. #3
    Junior Member p0oint's Avatar
    Join Date
    Jun 2010
    Location
    Macedonia
    Posts
    10
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Timer class countdown

    Quote Originally Posted by Norm View Post
    The red part is the body of the class 'a' that implements the ActionListener interface.
    Thanks. Is it a short way of writing class a implements ActionListener ?

    But how can you define class into constructor?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Timer class countdown

    To have a constructor use the normal way of defining a class.

  6. The Following User Says Thank You to Norm For This Useful Post:

    p0oint (August 26th, 2010)

  7. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Timer class countdown

    The ActionListener the code creates is called an anonymous inner class - a quick way to define a class or implement an interface, an advantage being its easy and has access to the outer class variables (via this.outerclassname.variable). Quite often the reference isn't needed, and the class defined when needed

      timer = new Timer(300, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            System.out.println("Counter = " + counter);
     
            if (++counter > 10) {
              timer.stop();
              System.exit(0);
            }
          }
        });

    The anonymous class is just that: no name class. The compiler is smart enough to treat this as a normal class, with a default constructor being called upon instantiation. If you need a constructor you may be better off defining either another class or inner class. See Inner Class Example (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Last edited by copeg; August 25th, 2010 at 09:19 PM.

  8. The Following User Says Thank You to copeg For This Useful Post:

    p0oint (August 26th, 2010)

  9. #6
    Junior Member p0oint's Avatar
    Join Date
    Jun 2010
    Location
    Macedonia
    Posts
    10
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Timer class countdown

    Thank you. I understand now what really is.

    So it is like
    class Anonymous implements ActionListener
    {
    public void actionPerformed(ActionEvent e) {
            System.out.println("Counter = " + counter);
     
            if (++counter > 10) {
              timer.stop();
              System.exit(0);
            }
          }
        }
    }
    With definition of the function actionPerformed(ActionEvent e)

    and then new Anonymous is put into the constructor of the Timer (300,new Anonymous), right?
    Last edited by p0oint; August 26th, 2010 at 03:43 AM.

Similar Threads

  1. How to Use Timer in Java
    By neo_2010 in forum Java SE API Tutorials
    Replies: 2
    Last Post: August 6th, 2013, 09:49 AM
  2. Timer Class help
    By Deadbob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 23rd, 2010, 12:18 AM
  3. need help with Timer and sound
    By amahara in forum AWT / Java Swing
    Replies: 4
    Last Post: February 18th, 2010, 12:22 PM
  4. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM
  5. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM

Tags for this Thread