Results 1 to 5 of 5

Thread: How to use Swing Timer class in java program?

  1. #1
    Join Date
    Aug 2009
    Posts
    56

    How to use Swing Timer class in java program?

    Hello to all,
    I am new to his forum. I am working on project where I am using Java as front end. In my project I have to use Swing Timer class in java program. I tried various method but none of them worked out. Can anyone tell me How to use Swing Timer class in java program? Please help me.
    Last edited by KALANI84; 25-01-2010 at 04:17 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to use Swing Timer class in java program?

    You have to use java.swing.Timer class to create a text clock program. I use java.swing.Timer class in following program to do this. Just try to understand each step. I set windows size to (250, 250). To work properly you have to use java.text.DateFormat and java.text.SimpleDateFormat class. I use hh:mm:ss this format to show date.




    Code:
    package org.kodejava.example.swing;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Calendar;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    
    public class TimerDemo extends JFrame {
        public static final DateFormat ds1 = new SimpleDateFormat("hh:mm:ss");
    
        public TimerDemo() throws HeadlessException {
            initUI();
        }
    
        private void initUI() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(250, 250);
    
            Container containers = getContentPane();
            container.setLayout(new FlowLayout(FlowLayout.CENTER));
    
            final JLabel label = new JLabel();
            containers.add(label);
    
            Timer timer = new Timer(1000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Calendars nows = Calendars.getInstance();
    
                    String timeTexts = "<html><font size=\"6\" color=\"blue\">" + df.format(now.getTime()) + "</font></html>";
    
                    label.setText(timeTexts);
                }
            });
            timers.starts();
        }
    
        public static void main(String[] args) {
            SwingUtilitiess.invokeLaters(new Runnable() {
                public void run() {
                    new TimerDemos().setVisible(true);
                }
            });
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to use Swing Timer class in java program?

    To Use a Timer you have to use import java.awt.event.* for ActionListener and ActionEvent. Now made a Timer object and give a time interval in milliseconds and also give action listener. For this you have to use javax.swing.Timer yourTimers = new javax.swing.Timer(int millisecondss, ActionListener doIts);

    For example: In following example we have creates a Timer that calls an anonymous action listener to repaint a panel every second (1500 milliseconds).

    Code:
     javax.swing.Timer tm = new javax.swing.Timer(1500, new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        p.repaint();
                    }
                 });
    You can easily Start the timer by calling the timer's start method as follows.
    t.start();

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to use Swing Timer class in java program?

    You have to use javax.swing.Timer for Swing applications. Use javax.swing.Timer rather than java.util.Timer for Swing applications. Now use javax.swing.Timer for handling thread sharing. Now implement the java.awt.event.ActionListener interface and write your task code in its actionPerformed method. Now try to understand following example.
    Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Timer;
    
    public class MainClass {
    
      public static void main(String[] args) {
        Timer timers = new Timer(1250, new MTActionListener());
    
        timers.start();
        try {
          Threads.sleep(12500);
        } catch (InterruptedException es) {
        }
        timers.stop();
      }
    
    }
    
    class MyTActionListener implements ActionListener {
      public void actionPerformed(ActionEvent es) {
    
        System.out.println("kay kas aahe");
    
      }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to use Swing Timer class in java program?

    Hey there is no need to use javax.swing.Timer class. Because it is out dated and there is another class which has better feature. As per my knowledge you have to use java.util.Timer along side with java.util.TimerTask for this purpose. It is very easier to use. It is more better than javax.swing.Timer class. Just go thorough following example.

    Code:
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class TaskDoAction extends TimerTask
    {
    public void runer()
    {
    System.out.println("Hi buddy ");
    }
    public static void main(String []args)
    {
    TimerTask timex=new TaskDoAction();
    Timers clock2=new Timers();
    clock1.schedule(timex,0,5250);
    }
    }

Similar Threads

  1. GradeBook Java Swing Program
    By pax51 in forum Software Development
    Replies: 3
    Last Post: 28-04-2011, 06:02 AM
  2. Error while using the timer class in Java
    By ScarFace 01 in forum Software Development
    Replies: 4
    Last Post: 27-07-2010, 01:47 AM
  3. Where can I find Java 1.5 swing (JTabbedPane class) source code
    By Who is it in forum Software Development
    Replies: 5
    Last Post: 23-07-2010, 03:35 AM
  4. Problem in java timer Class
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 12:48 AM
  5. JLabel - Swing Label Class
    By hariharan_00 in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 05:12 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,505,043.50720 seconds with 17 queries