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

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by bullet
    You essentially have the same problem as before.

    The easiest away to remedy it would be to create a seperate listener for the Timer.
    Created a seperate listener for timer, added and initialized new timer and started timer in timerListener() class. Still same output. BTW, where do I put repaint()? in timerListener, or leave where it is?

    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 timerListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            { 
                timer = new Timer(20, new timerListener());
                timer.start();       
            }
        }
        
         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. #17
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Note that the code where you create the timer will never execute.

    What I meant was leave the lines where you create the timer in the actionPerformed of buttonListener, but put the code that changes the labels into the actionPerformed of another listener.
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by bullet
    Note that the code where you create the timer will never execute.

    What I meant was leave the lines where you create the timer in the actionPerformed of buttonListener, but put the code that changes the labels into the actionPerformed of another listener.
    Like this? I put timerListener() inside the new timer;

    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 timerListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            { 
            
            }
        }
        
         private class buttonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            { 
    
                if(event.getSource()==start)
                {
                     timer = new Timer(20, new timerListener());
                     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();
                }
          
            }
        }
        
    }
  6. #19
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Yes, but now you need the code that changes the labels in the timerListener.
  8. #20
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by bullet
    Yes, but now you need the code that changes the labels in the timerListener.
    Did that, but now everything is incremented by one, 10 became 11, 60 becomes 61. Other than that, still the same result. How do you add pictures to the threads so I can show you the results?

    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 timerListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            { 
                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();
            
            }
        }
        
         private class buttonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            { 
    
                if(event.getSource()==start)
                {
                     timer = new Timer(20, new timerListener());
                     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();
                }
          
            }
        }
        
    }
  10. #21
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    I would put all of the code that is currently in the buttonListener that changes the labels and put it in timerListener

    The only things that should be in the buttonListener are the creation of the timer and the line that starts it.
  12. #22
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by bullet
    I would put all of the code that is currently in the buttonListener that changes the labels and put it in timerListener

    The only things that should be in the buttonListener are the creation of the timer and the line that starts it.
    Still back to the same problem as before. is the repaint() at the right place, is it the correct function? Should I do like,

    hoursLabel.repaint();
    minutesLabel.repaint();
    secondsLabel.repaint();

    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 timerListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            { 
                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();
            }
        }
        
         private class buttonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent event)
            { 
    
                if(event.getSource()==start)
                {
                     timer = new Timer(20, new timerListener());
                     timer.start();
    
                }
          
            }
        }
        
    }
  14. #23
  15. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    What is supposed to be happening?
  16. #24
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by bullet
    What is supposed to be happening?


    you can see what I see now, just right click on the image icon, and open in a new tab.

    after, I compile the java file, a frame with a button opens.
    When I click the button, I'm suppose to see a counter

    hours
    minutes
    seconds

    so far I see just 10 for hours, 60 for minutes and seconds. I would like to see the actual count down in the frame.
  18. #25
  19. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    This is where you use MrFujin's comment.

    You do have a delay in the Timer, but the delay is between calls to the actionPerformed. Because you have no delays in the code where you update the labels, you aren't going to see them change.

    You will need to all a call to Thread.sleep in the loop.
  20. #26
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by bullet
    This is where you use MrFujin's comment.

    You do have a delay in the Timer, but the delay is between calls to the actionPerformed. Because you have no delays in the code where you update the labels, you aren't going to see them change.

    You will need to all a call to Thread.sleep in the loop.
    Thank you, I'll look into that. So far, I have no idea what a thread.call is, I don't remember reading about it.

    Thanks for your help.
  22. #27
  23. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Originally Posted by tonynsx
    Thank you, I'll look into that. So far, I have no idea what a thread.call is, I don't remember reading about it.

    Thanks for your help.
    The sleep method is a static method in the Thread class. It causes whatever thread that executes it to pause for a number of milliseconds. It does throw a checked exception, InterruptedException, so you can use a try catch when you call it.
  24. #28
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by bullet
    The sleep method is a static method in the Thread class. It causes whatever thread that executes it to pause for a number of milliseconds. It does throw a checked exception, InterruptedException, so you can use a try catch when you call it.
    Thank you,

    I'm looking on youtube and online about the thread class. I'll restart my program once I figure out how to use it.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo