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 3 of 3

Thread: Tell java to do a something when timer ticks

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Tell java to do a something when timer ticks

    I created a tick class that ticks every second:
    package com.dayz.testmod;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.Timer;
     
    public class Tick {
     
    	ActionListener action = new ActionListener(){
     
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    		System.out.println("Tick!");	
    		}
     
    	};
     
    	Timer t = new Timer(2000, action);
    	public void start(){
    		t.start();
    I want java to do something when it ticks without putting it in the action performed method.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    851
    Thanks
    0
    Thanked 158 Times in 141 Posts

    Default Re: Tell java to do a something when timer ticks

    That would not be possible. The ActionListener is the only way to know when the timer has run through.

    But I guess what you want is to have your own Listener class that listens to ticks of your Tick class. If this is the case you should probably read a little bit about the observer-pattern and maybe look at some example implementation of other classes. Observers (in swing they are called listeners) are a very common programming standard.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,097
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: Tell java to do a something when timer ticks

    You might also consider an EventBus design.

Similar Threads

  1. [SOLVED] both class javax.swing.Timer in javax.swing and class java.util.Timer in java.util match
    By stresstedout in forum What's Wrong With My Code?
    Replies: 13
    Last Post: April 10th, 2014, 08:32 PM
  2. How to Use Timer in Java
    By neo_2010 in forum Java SE API Tutorials
    Replies: 2
    Last Post: August 6th, 2013, 10:49 AM
  3. Is this even a valid way of getting Ticks Per Second?
    By vividMario52 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2013, 08:25 AM
  4. How to Use Timer in Java
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: September 1st, 2009, 01:10 PM
  5. How to Use Timer in Java
    By neo_2010 in forum Java Programming Tutorials
    Replies: 0
    Last Post: September 1st, 2009, 01:10 PM