Results 1 to 10 of 10

Thread: Java Timer

  1. #1
    Oliver is offline Member
    Join Date
    Sep 2015
    Posts
    5
    Rep Power
    0

    Question Java Timer

    Hi guys, I need help with the Timer in java.
    For this program, i ave to allow the user to select one of three countdowns: 20 seconds, 1 minute, 3 minutes. This should be done using a set of radio buttons. Then, I have to use a Start button to begin the countdown.After that, I have to use a timer object to control the countdown. Display the countdown in large digits, updating it every second. Finally, I have to use a Stop button to allow the user to end the countdown prematurely. When the Stop button is clicked, the time displayed should be reset to zero.

    I am using one listener to select one of the three countdowns. I am using a second listener to use the start button and begin the countdown. I am having trouble to the start button, it does not work after I click on it. In addition, I do not know how to do the countdown.

    I would appreciate any idea or hint...Thanks

    Here is the code for the JFrame file:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class DriverLayoutManager
    {
    	public static void main(String[]args)
    	{
    		JFrame frame = new JFrame("Countdown");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		frame.getContentPane().add(new layoutPanel());
    
    		frame.pack();
    		frame.setVisible(true);
    
    	}
    }
    Here is the code from the layoutPanel():

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.Timer;
    import javax.swing.event.ChangeListener;
    
    public class layoutPanel extends JPanel
    {
    	private boolean on;
    	private ImageIcon image;
    	private JLabel countDown;
    	private JRadioButton twentySeconds, oneMinute, threeMinutes;
    	private JButton btnStart, btnStop;
    	private Timer timer;
    	private JLabel imageLabel;
    	private int counter=0,seconds;
    	private int ts=20, om, tm;
    
    	public layoutPanel()
    	{
    		setLayout(new FlowLayout());
    		setBackground(Color.blue);
    
    
    		image = new ImageIcon("concert.GIF");
    		countDown = new JLabel("00:00");
    		countDown.setPreferredSize(new Dimension (200, 100));
    		countDown.setBorder(BorderFactory.createEtchedBorder());
    		countDown.setFont(new Font("Arial", Font.BOLD, 76));
    
    		twentySeconds = new JRadioButton("Twenty");
    		oneMinute = new JRadioButton("One");
    		threeMinutes = new JRadioButton("Three");
    
    		ButtonGroup group = new ButtonGroup();
    		group.add(twentySeconds);
    		group.add(oneMinute);
    		group.add(threeMinutes);
    		twentySeconds.addActionListener(new timerListener());
    		oneMinute.addActionListener(new timerListener());
    		threeMinutes.addActionListener(new timerListener());
    
    		btnStart = new JButton("Start");
    		btnStart.addActionListener(new ButtonListener());
    		btnStop = new JButton("Stop");
    		btnStop.addActionListener(new ButtonListener());
    
    		timer = new Timer(1000, new timerListener());
    		timer.addActionListener(new timerListener());
    		on = true;
    		imageLabel = new JLabel(image);
    
    		add(imageLabel);
    		add(countDown);
    		add(twentySeconds);
    		add(oneMinute);
    		add(threeMinutes);
    		add(btnStart);
    		add(btnStop);
    
    		setPreferredSize(new Dimension(250,400));
    
    
    	}
    	//Represent the Action listener for the timer
    	private class timerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			if(twentySeconds.isSelected())
    			{
    			countDown.setText("00:20");
    			ts=20;
    			}
    			if(oneMinute.isSelected())
    			{
    			countDown.setText("01:00");
    			om = 60;
    			}
    			if(threeMinutes.isSelected())
    			{
    			countDown.setText("03:00");
    			tm = 180;
    			}
    
    
    		}
    	}
    	private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    
    		//I supposed to get the data from timerListener
    			if(btnStart.isSelected())
    			{
    				timer.start();
    				if(ts!=0)
    				countDown.setText("00:"+ts);
    				ts--;
    
    
    			}
    
    		}
    	}
    
    }
    Last edited by Oliver; 09-05-2015 at 04:53 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java Timer

    Please edit your post and wrap the code in code tags to make it easier to read and understand.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Oliver is offline Member
    Join Date
    Sep 2015
    Posts
    5
    Rep Power
    0

    Default Re: Java Timer

    What do you mean?

    Like this?
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.Timer;
    import javax.swing.event.ChangeListener;
    
    public class layoutPanel extends JPanel
    {
    	private boolean on;
    	private ImageIcon image;
    	private JLabel countDown;
    	private JRadioButton twentySeconds, oneMinute, threeMinutes;
    	private JButton btnStart, btnStop;
    	private Timer timer;
    	private JLabel imageLabel;
    	private int counter=0,seconds;
    	private int ts=20, om, tm;
    
    	public layoutPanel()
    	{
    		setLayout(new FlowLayout());
    		setBackground(Color.blue);
    
    // constructor
    		image = new ImageIcon("concert.GIF");
    		countDown = new JLabel("00:00");
    		countDown.setPreferredSize(new Dimension (200, 100));
    		countDown.setBorder(BorderFactory.createEtchedBorder());
    		countDown.setFont(new Font("Arial", Font.BOLD, 76));
    
    		twentySeconds = new JRadioButton("Twenty");
    		oneMinute = new JRadioButton("One");
    		threeMinutes = new JRadioButton("Three");
    
    		ButtonGroup group = new ButtonGroup();
    		group.add(twentySeconds);
    		group.add(oneMinute);
    		group.add(threeMinutes);
    		twentySeconds.addActionListener(new timerListener());
    		oneMinute.addActionListener(new timerListener());
    		threeMinutes.addActionListener(new timerListener());
    
    		btnStart = new JButton("Start");
    		btnStart.addActionListener(new ButtonListener());
    		btnStop = new JButton("Stop");
    		btnStop.addActionListener(new ButtonListener());
    
    		timer = new Timer(1000, new timerListener());
    		timer.addActionListener(new timerListener());
    		on = true;
    		imageLabel = new JLabel(image);
    
    		add(imageLabel);
    		add(countDown);
    		add(twentySeconds);
    		add(oneMinute);
    		add(threeMinutes);
    		add(btnStart);
    		add(btnStop);
    
    		setPreferredSize(new Dimension(250,400));
    
    
    	}
    	//Represent the Action listener for the Radio buttons
    	//to select one of three countdowns: 20 seconds, 1 minute, 3 minutes.
    	private class timerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			if(twentySeconds.isSelected())
    			{
    			countDown.setText("00:20");
    			ts=20;
    			}
    			if(oneMinute.isSelected())
    			{
    			countDown.setText("01:00");
    			om = 60;
    			}
    			if(threeMinutes.isSelected())
    			{
    			countDown.setText("03:00");
    			tm = 180;
    			}
    
    
    		}
    	}
    	
    	////Represent the Action listener for the Start button after one of the Radio buttons is clicked
    	private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    
    		//I supposed to get the data from timerListener
    			if(btnStart.isSelected())
    			{
    				timer.start();
    				if(ts!=0)
    				countDown.setText("00:"+ts);
    				ts--;
    
    
    			}
    
    		}
    	}
    
    
    }
    Last edited by Oliver; 09-05-2015 at 04:48 AM.

  4. #4
    Oliver is offline Member
    Join Date
    Sep 2015
    Posts
    5
    Rep Power
    0

    Default Re: Java Timer

    Hi
    what do you mean by "wrap the code in code tags"

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java Timer

    Like this:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    Java Code:
      it should look
      like this
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Oliver is offline Member
    Join Date
    Sep 2015
    Posts
    5
    Rep Power
    0

    Default Re: Java Timer

    Thanks for telling me about that. I am new in this forum.
    I do not know why the start button does not work inside the listener.
    I also do not know how to countdown with the timer
    Last edited by Oliver; 09-05-2015 at 04:58 AM.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Java Timer

    Moved from Advanced Java to AWT/Swing.

    Have you discovered the Oracle tutorials? How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    db

    edit You also need to go through http://docs.oracle.com/javase/tutori...ts/button.html
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java Timer

    why the start button does not work inside the listener.
    Check if the listener is being called by adding a println() statement first thing in the listener that prints out a message showing it is being executed.

    The code is easier to read with the formatting.
    Fix the indentation of statements within if statements. They should be indented to make it easier to see.

    Some suggestions:
    Add {}s after all the if statements
    Make a separate listener for the Timer class to use. Don't mix button click handling with Timer events.
    Use a single variable for the countdown time, not 3. Use a variable name with meaning, not om or ts.
    Disable the Start and Stop buttons until the user has chosen the duration of the countdown.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Oliver is offline Member
    Join Date
    Sep 2015
    Posts
    5
    Rep Power
    0

    Default Re: Java Timer

    Hi Norm, thanks for helping me.
    I already added the println() statement, and it is not printing anything on the screen.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java Timer

    it is not printing anything on the screen.
    That would mean it is not being executed.

    Please post the new code with the println() statement and the suggested changes.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. need help with the Timer in java.
    By couline in forum New To Java
    Replies: 1
    Last Post: 03-21-2012, 04:21 PM
  2. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  3. Need Help with timer (Java)
    By acash229 in forum New To Java
    Replies: 58
    Last Post: 09-13-2010, 01:32 PM
  4. Timer in java
    By manhit45 in forum New To Java
    Replies: 4
    Last Post: 12-14-2009, 02:27 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 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
  •