Results 1 to 10 of 10
  1. #1
    morfasto is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default Swing Timer Questions

    Hello!

    I found a code that helped me a lot on my project, but I need to learn a few more things... This is my code:

    Java Code:
    private void jButton1_actionPerformed(ActionEvent e) {
            Timer timer = new Timer (1000, new ActionListener ()
                    {
                        int seg=0;
                        public void actionPerformed(ActionEvent f)
                        {         
                           jTextField1.setText("Seconds: "+seg)
                        }
                    });
    
                    timer.start();
           
            }
    1) I want to pause or stop that timer with another jButton, how do I do that?
    2) I want to create another jButton like this:

    Java Code:
    private void jButton3_actionPerformed(ActionEvent e) {
            Timer timer = new Timer (1000, new ActionListener ()
                    {
                        int seg=0;
                        public void actionPerformed(ActionEvent f)
                        {         
                           jTextField2.setText("Seconds: "+seg)
                        }
                    });
    
                    timer.start();
           
            }
    So I can have 2 labels counting in different moments, how do I do that?

    Thank you for your help.

    PS Im noob, sorry.

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

    Default

    Get the Timer objects out of the button's actionPerformed method and make them class variables. Then one button can start the button and another button can pause the timer. Also, don't you need to increment the seg variable from within the Timer's actionPerformed method?

  3. #3
    morfasto is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    Could you please tell me or give me an example of how to do that?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here is the basic idea about what Fubarable says. Basically he's talking about the scope of the object variables.

    Java Code:
    public class TimerDemo {
    
        JButton btnStart;
        JButton btnStop;
        Timer timer;
    
        public TimerDemo() {
            btnStart = new JButton("Start");
            btnStop = new JButton("Stop");
    
            ActionListener a = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Timer Started");
                }
            };
    
            // Initialize the timer
            timer = new Timer(5000, a);
            
            // Events
            btnStart.addMouseListener(new MouseAdapter() {
                public void  mouseClicked(java.awt.event.MouseEvent evt) {
                    timer.start();
                }
            });
    
        }

  5. #5
    morfasto is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post

    Java Code:
    public class TimerDemo {
    
        JButton btnStart;
        JButton btnStop;
        Timer timer;
    
        public TimerDemo() {
            btnStart = new JButton("Start");
            btnStop = new JButton("Stop");
    
            ActionListener a = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Timer Started");
                }
            };
    
            // Initialize the timer
            timer = new Timer(5000, a);
            
            // Events
            btnStart.addMouseListener(new MouseAdapter() {
                public void  mouseClicked(java.awt.event.MouseEvent evt) {
                    timer.start();
                }
            });
    
        }
    Ok, thats the class, but how do I implement the class on the frame? How do I call the btnStart and btnStop? and where do I put the code of the thing I want to run?

    Thanks

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

    Default

    You don't "call" btnStart or btnStop. Those are JButtons, and you will place code to start and stop the timers within their action listeners. You may benefit by reading through the Sun Swing tutorial on JButtons, ActionListeners and Swing Timers as it will prove very instructive for you. Much luck!

  7. #7
    morfasto is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    Where do I find those tutorials?

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

  9. #9
    morfasto is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    Thank you for being so helpful =)

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 02:45 AM
  2. Timer on swing
    By finzaiko in forum AWT / Swing
    Replies: 3
    Last Post: 04-02-2009, 08:45 AM
  3. Timer in Swing app - refreshing label
    By qoqosz in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 03:17 AM
  4. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 01:25 PM
  5. Two swing questions
    By carderne in forum New To Java
    Replies: 10
    Last Post: 12-24-2008, 06:01 PM

Posting Permissions

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