Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1

    Help with timer class, updating JLabel each time


    Hello,

    I'm writing this counter program. Basically it counts down the hours, minutes and seconds. I created a for loop for each. I set their values to 3 JLabels for hours, minutes and seconds. How can I show it that it updates their values in real time? Now all 3 JLabels show 10, 60 and 60.

    I want it to show

    1 : 1 : 1
    1 : 1 : 2
    1 : 1 : 3
    1 : 1 : 4 .... so on

    Code:
    package digitalcounter;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class digitalCounterPanel extends JPanel{
        
        private JButton start;
        private JPanel buttonPanel, displayPanel;
        private JLabel hoursLabel, minutesLabel, secondsLabel;
        private int hours, minutes, seconds;
        private Timer timer;
    
        public digitalCounterPanel()
        {   
            timer = new Timer(20, new buttonListener());
            
            buttonPanel = new JPanel();
            buttonPanel.setPreferredSize(new Dimension(150, 150));
            buttonPanel.setBackground(Color.white);
            displayPanel = new JPanel();
            displayPanel.setPreferredSize(new Dimension(350, 150));
            displayPanel.setBackground(Color.white);
            
            start = new JButton(new ImageIcon("start.jpg"));
            start.addActionListener(new buttonListener());
            buttonPanel.add(start);
            
            hoursLabel = new JLabel();
            hoursLabel.setPreferredSize(new Dimension(100,140));
            minutesLabel = new JLabel();
            minutesLabel.setPreferredSize(new Dimension(100,140));
            secondsLabel = new JLabel();
            secondsLabel.setPreferredSize(new Dimension(100,140));
            
            displayPanel.add(hoursLabel);
            displayPanel.add(minutesLabel);
            displayPanel.add(secondsLabel);
                    
            add(buttonPanel);
            add(displayPanel);
            
            setBackground(Color.black);    
            
            timer.start();
        }
        
        public void paintComponent (Graphics updateLabels)
        {
            super.paintComponent(updateLabels);
         // what do I put here to update the labels?
          
        }
    
         private class buttonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            { 
                if(event.getSource()==start)
                {
                    for(hours=1; hours<=10; hours++)
                    {
                        for(minutes=1; minutes<=60; minutes++)
                        {
                            for(seconds=1; seconds<=60; seconds++)
                            {
                                
                                hoursLabel.setFont(new Font("Helvetica", Font.BOLD, 84));
                                minutesLabel.setFont(new Font("Helvetica", Font.BOLD, 84));
                                secondsLabel.setFont(new Font("Helvetica", Font.BOLD, 84));
                                
                                hoursLabel.setText(Integer.toString(hours));
                                minutesLabel.setText(Integer.toString(minutes));
                                secondsLabel.setText(Integer.toString(seconds));
                                
                            }
                        }
                    }
                    
                    repaint();
                }
          
            }
        }
        
    }
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    You have to tell it if you only want it to update/execute it once per second.
    Maybe you can take a look at Thread.sleep.

    Just noticed that this probably will "lock down" the whole application as you only use one (main) thread now.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by MrFujin
    You have to tell it if you only want it to update/execute it once per second.
    Maybe you can take a look at Thread.sleep.

    Just noticed that this probably will "lock down" the whole application as you only use one (main) thread now.
    I'm sorry but, I don't understand what you meant by Thread.sleep. As far as I know, I did tell it when I want it to execute(20 milliseconds).
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    One thing I notice is this in your actionPerformed

    Code:
    if(event.getSource()==start)
                {
    This means that the code will only execute if the source of the event is the button named start.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by bullet
    One thing I notice is this in your actionPerformed

    Code:
    if(event.getSource()==start)
                {
    This means that the code will only execute if the source of the event is the button named start.
    That's correct. I only want it to execute the actionPerformed when I click the button.
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by tonynsx
    That's correct. I only want it to execute the actionPerformed when I click the button.
    But you also make the buttonListener the action of the Timer
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by bullet
    But you also make the buttonListener the action of the Timer
    I'm going by an example on my text book, and that's how they have it.

    Code:
    package rebound;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class reboundPanel extends JPanel{
        
        private final int WIDTH = 300, HEIGHT = 100;
        private final int DELAY = 20, IMAGE_SIZE = 35;
        
        private ImageIcon image;
        private Timer timer;
        private int x, y, moveX, moveY;
        
        public reboundPanel()
        {
         timer = new Timer(DELAY, new reboundListener());
         
         image = new ImageIcon("happyFace.jpg");
         
         x = 0;
         y = 40;
         moveX = moveY = 3;
         
         setPreferredSize(new Dimension(WIDTH, HEIGHT));
         setBackground(Color.black);
         timer.start();
        }
        
        public void paintComponent (Graphics panel)
        {
            super.paintComponent(panel);
            image.paintIcon(this, panel, x, y);
        }
        
        private class reboundListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            {
                x = x + moveX;
                y = y + moveY;
                
                if(x <=0 || x >= WIDTH - IMAGE_SIZE)
                    moveX = moveX * -1;
                
                if(y <=0 || y >= HEIGHT - IMAGE_SIZE)
                    moveY = moveY * -1;
                
                repaint();
            }
        }
    }
  14. #8
  15. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    But the difference in your code is that you are using the same listener for the timer and the button.

    When the timer starts, it will fire the actionPerformed every 20 milliseconds. But if the code in the actionPerformed only happens if the button is clicked, nothing will happen when the timer fires.
    Last edited by bullet; August 20th, 2013 at 10:19 AM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by bullet
    But the difference in your code is that you are using the same listener for the timer and the button.

    When the timer starts, it will fire the actionPerformed every 20 milliseconds. But if the code in the actionPerformed only happens if the button is clicked, nothing will happen when the timer fires.
    so can I put

    Code:
    timer = new Timer(20, new reboundListener());
    timer.start();
    inside the reboundListener class?

    In the textbook example, there's no buttons, so the program runs and "animates" when the JFrame loads, is that why they have the
    Code:
    timer = new Timer(20, new reboundListener());
    timer.start();
    inside the public reboundPanel()?

    since I only want the action to happen after I click the button, I can just put
    Code:
    timer = new Timer(20, new reboundListener());
    timer.start();
    inside the reboundListener() class?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    New code:

    put newTimer() and stated the timer in actionPerformed

    Code:
    package digitalcounter;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class digitalCounterPanel extends JPanel{
        
        private JButton start;
        private JPanel buttonPanel, displayPanel;
        private JLabel hoursLabel, minutesLabel, secondsLabel;
        private int hours, minutes, seconds;
        private Timer timer;
    
        public digitalCounterPanel()
        {          
            buttonPanel = new JPanel();
            buttonPanel.setPreferredSize(new Dimension(150, 150));
            buttonPanel.setBackground(Color.white);
            displayPanel = new JPanel();
            displayPanel.setPreferredSize(new Dimension(350, 150));
            displayPanel.setBackground(Color.white);
            
            start = new JButton(new ImageIcon("start.jpg"));
            start.addActionListener(new buttonListener());
            buttonPanel.add(start);
            
            hoursLabel = new JLabel();
            hoursLabel.setPreferredSize(new Dimension(100,140));
            minutesLabel = new JLabel();
            minutesLabel.setPreferredSize(new Dimension(100,140));
            secondsLabel = new JLabel();
            secondsLabel.setPreferredSize(new Dimension(100,140));
            
            displayPanel.add(hoursLabel);
            displayPanel.add(minutesLabel);
            displayPanel.add(secondsLabel);
                    
            add(buttonPanel);
            add(displayPanel);
            
            setBackground(Color.black);    
        }
        
        public void paintComponent (Graphics updateLabels)
        {
            super.paintComponent(updateLabels);
         // what do I put here to update the labels?
          
        }
    
         private class buttonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            { 
                timer = new Timer(20, new buttonListener());
                timer.start();
                
                if(event.getSource()==start)
                {
                    for(hours=1; hours<=10; hours++)
                    {
                        for(minutes=1; minutes<=60; minutes++)
                        {
                            for(seconds=1; seconds<=60; seconds++)
                            {
                                
                                hoursLabel.setFont(new Font("Helvetica", Font.BOLD, 84));
                                minutesLabel.setFont(new Font("Helvetica", Font.BOLD, 84));
                                secondsLabel.setFont(new Font("Helvetica", Font.BOLD, 84));
                                
                                hoursLabel.setText(Integer.toString(hours));
                                minutesLabel.setText(Integer.toString(minutes));
                                secondsLabel.setText(Integer.toString(seconds));
                                
                            }
                        }
                    }
                    
                    repaint();
                }
          
            }
        }
        
    }
  20. #11
  21. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Do you only want the timer to start if the button is clicked?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by bullet
    Do you only want the timer to start if the button is clicked?
    yes, also do I even need this in my code? Since hours, minutes and seconds are going to their own JLabels? I commented the code below, and it's still showing the same results, that's why I'm wondering. Thanks for your help BTW.

    Code:
        public void paintComponent (Graphics updateLabels)
        {
            super.paintComponent(updateLabels);
         // what do I put here to update the labels?
          
        }
  24. #13
  25. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    No, the paintComponent is not needed here.

    In the actionPerformed, I would put the declaration of the timer, and its start in the body of the if statement. That way, if you add other buttons, they won't cause the timer to start.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by bullet
    No, the paintComponent is not needed here.

    In the actionPerformed, I would put the declaration of the timer, and its start in the body of the if statement. That way, if you add other buttons, they won't cause the timer to start.
    I declared and started the timer in the body of the if statements, but still same results. I wish there was a way to post picture in this thread.

    Code:
    package digitalcounter;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class digitalCounterPanel extends JPanel{
        
        private JButton start;
        private JPanel buttonPanel, displayPanel;
        private JLabel hoursLabel, minutesLabel, secondsLabel;
        private int hours, minutes, seconds;
        private Timer timer;
    
        public digitalCounterPanel()
        {          
            buttonPanel = new JPanel();
            buttonPanel.setPreferredSize(new Dimension(150, 150));
            buttonPanel.setBackground(Color.white);
            displayPanel = new JPanel();
            displayPanel.setPreferredSize(new Dimension(350, 150));
            displayPanel.setBackground(Color.white);
            
            start = new JButton(new ImageIcon("start.jpg"));
            start.addActionListener(new buttonListener());
            buttonPanel.add(start);
            
            hoursLabel = new JLabel();
            hoursLabel.setPreferredSize(new Dimension(100,140));
            minutesLabel = new JLabel();
            minutesLabel.setPreferredSize(new Dimension(100,140));
            secondsLabel = new JLabel();
            secondsLabel.setPreferredSize(new Dimension(100,140));
            
            displayPanel.add(hoursLabel);
            displayPanel.add(minutesLabel);
            displayPanel.add(secondsLabel);
                    
            add(buttonPanel);
            add(displayPanel);
            
            setBackground(Color.black);    
        }
    
         private class buttonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            { 
    
                if(event.getSource()==start)
                {
                     timer = new Timer(20, new buttonListener());
                     timer.start();
                
                    for(hours=1; hours<=10; hours++)
                    {
                        for(minutes=1; minutes<=60; minutes++)
                        {
                            for(seconds=1; seconds<=60; seconds++)
                            {
                                
                                hoursLabel.setFont(new Font("Helvetica", Font.BOLD, 84));
                                minutesLabel.setFont(new Font("Helvetica", Font.BOLD, 84));
                                secondsLabel.setFont(new Font("Helvetica", Font.BOLD, 84));
                                
                                hoursLabel.setText(Integer.toString(hours));
                                minutesLabel.setText(Integer.toString(minutes));
                                secondsLabel.setText(Integer.toString(seconds));
                                
                            }
                        }
                    }
                    
                    repaint();
                }
          
            }
        }
        
    }
  28. #15
  29. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    You essentially have the same problem as before.

    The easiest away to remedy it would be to create a seperate listener for the Timer.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo