Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Timer Action Listener

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Timer Action Listener

    Whats wrong with my code ?? Whenever i click start button it will just printout start to the jtextfield it wont start the time .
    import java.awt.*;
    import java.util.Date;
     
    import javax.swing.*;
     
    import java.awt.BorderLayout;
     
    import java.awt.Dimension;
    import java.awt.event.*;
     
     
    public class MicroWave extends JFrame implements ActionListener {
     
     
     
    		  JButton n0,n1,n2,n3,n4;
    		  JButton n5,n6,n7,n8,n9;
    		  JButton start,stop;
    		   JPanel keyButtons;
     
    		  JPanel South;
    		  JPanel North;
    		  JPanel West;
    		  JPanel East;
    		  JPanel time;
    		Timer starter;
    		  JTextField screen;
     
    			  long cd = 1800000; 
    			   java.text.SimpleDateFormat tformat = new java.text.SimpleDateFormat(" hh  : mm : ss");  
    			     JTextField timer = new JTextField(tformat.format(new Date(cd)),JTextField.CENTER);  
     
    			public void actionPerformed(ActionEvent arg0) {
     
    				if(timer.getText().equals("00:00:00"))
    				{
    					timer.setText("");
    					JButton source = (JButton)arg0.getSource();
    					timer.replaceSelection(arg0.getActionCommand());
    				}
    				else{
    					JButton source = (JButton)arg0.getSource();
    					timer.replaceSelection(arg0.getActionCommand());
    				}
     
    				 if(arg0.getActionCommand()=="Start"){
    				 ActionListener al = new ActionListener(){  
    				      long x = cd - 1000;  
    				      public void actionPerformed(ActionEvent ae){ 
     
     
    				    	  timer.setText(tformat.format(new Date(x)));  
    				          x -= 1000;}};  
    				     starter = new Timer(1000, al);
    				     starter.start();  
    				      }
    			}
     
    		public MicroWave(){
     
     
     
     
     
     
    		n1 = new JButton("1"); 	
    		n2 = new JButton("2"); 
    		n3 = new JButton("3"); 
    		n4 = new JButton("4"); 
    		n5 = new JButton("5"); 
    		n6 = new JButton("6"); 
    		n7 = new JButton("7"); 
    		n8 = new JButton("8"); 
    		n9 = new JButton("9"); 
    		start = new JButton("START"); 
    		n0 = new JButton("0"); 
    		stop = new JButton("STOP"); 
     
    		North= new JPanel(new BorderLayout(260,0));
    		South= new JPanel(new FlowLayout(FlowLayout.RIGHT));
    		West = new JPanel(new FlowLayout(FlowLayout.LEFT));
    		East = new JPanel(new FlowLayout(FlowLayout.RIGHT));
     
     
    		keyButtons = new JPanel();
     
    		time = new JPanel();
    		screen = new JTextField("SCREEN");
     
     
     
     
    		West.add(screen);
    		time.add(timer);
     
    		North.add(time,BorderLayout.EAST);
    		South = new JPanel();
     
    	}
     
    		public void launchFrame(){
     
    			keyButtons.setLayout(new GridLayout(4,3));
     
    			South.setPreferredSize(new Dimension(100,100));
    			screen.setPreferredSize(new Dimension(440,700));
    			timer.setPreferredSize(new Dimension(227,70));
    			keyButtons.setPreferredSize(new Dimension(235,250));
    			screen.add(new JTextField(8));
    			//INPUT WILL START AT THE CENTER
    			 timer.setHorizontalAlignment(JTextField.CENTER);
    			 screen.setHorizontalAlignment(JTextField.CENTER);
    			 //COLOR FOR JTEXTFIELD BACKGROUND//
    			 timer.setBackground(Color.DARK_GRAY); 
    			 screen.setBackground(Color.DARK_GRAY); 
    			 //FONT COLOR //
    			 screen.setForeground(Color.RED);  
    			 timer.setForeground(Color.RED);
    			 //FONT SIZE//
    				screen.setFont(new Font("Tahoma",1,55));
    				timer.setFont(new Font("BOLD",1,35));
     
    			keyButtons.add(n1);
    			keyButtons.add(n2);
    			keyButtons.add(n3);
    			keyButtons.add(n4);
    			keyButtons.add(n5);
    			keyButtons.add(n6);
    			keyButtons.add(n7);
    			keyButtons.add(n8);
    			keyButtons.add(n9);
    			keyButtons.add(start);
    			keyButtons.add(n0);
    			keyButtons.add(stop);
     
     
     
     
     
    			n1.addActionListener(this);
    			n2.addActionListener(this);
    			n3.addActionListener(this);
    			n4.addActionListener(this);
    			n5.addActionListener(this);
    			n6.addActionListener(this);
    			n7.addActionListener(this);
    			n8.addActionListener(this);
    			n9.addActionListener(this);
    			n0.addActionListener(this);
    			start.addActionListener(this);
    			stop.addActionListener(this);
     
     
     
     
     
     
    			JFrame dos = new JFrame();
    			dos.setSize(700,500);
    			dos.add(keyButtons,BorderLayout.EAST);
     
    			dos.add(screen,BorderLayout.WEST);
     
    			dos.add(North,BorderLayout.NORTH);
    			dos.add(South,BorderLayout.SOUTH);
    			dos.setVisible(true);
    			dos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
     
    		}
     
     
    		public static void main (String args[]){
    			MicroWave GUI = new MicroWave();
    			GUI.launchFrame();
     
     
     
     
     
     
    		}
     
     
     
     
    }


    --- Update ---

    if i remove IF S TATEMET it will start the countdown even if i click any jbuttons
    import java.awt.*;
    import java.util.Date;
     
    import javax.swing.*;
     
    import java.awt.BorderLayout;
     
    import java.awt.Dimension;
    import java.awt.event.*;
     
     
    public class MicroWave extends JFrame implements ActionListener {
     
     
     
    		  JButton n0,n1,n2,n3,n4;
    		  JButton n5,n6,n7,n8,n9;
    		  JButton start,stop;
    		   JPanel keyButtons;
     
    		  JPanel South;
    		  JPanel North;
    		  JPanel West;
    		  JPanel East;
    		  JPanel time;
    		Timer starter;
    		  JTextField screen;
     
    			  long cd = 1800000; 
    			   java.text.SimpleDateFormat tformat = new java.text.SimpleDateFormat(" hh  : mm : ss");  
    			     JTextField timer = new JTextField(tformat.format(new Date(cd)),JTextField.CENTER);  
     
    			public void actionPerformed(ActionEvent arg0) {
     
    				if(timer.getText().equals("00:00:00"))
    				{
    					timer.setText("");
    					JButton source = (JButton)arg0.getSource();
    					timer.replaceSelection(arg0.getActionCommand());
    				}
    				else{
    					JButton source = (JButton)arg0.getSource();
    					timer.replaceSelection(arg0.getActionCommand());
    				}
     
     
    				 ActionListener al = new ActionListener(){  
    				      long x = cd - 1000;  
    				      public void actionPerformed(ActionEvent ae){ 
     
     
    				    	  timer.setText(tformat.format(new Date(x)));  
    				          x -= 1000;}};  
    				     starter = new Timer(1000, al);
    				     starter.start();  
    				      }
     
     
    		public MicroWave(){
     
     
     
     
     
     
    		n1 = new JButton("1"); 	
    		n2 = new JButton("2"); 
    		n3 = new JButton("3"); 
    		n4 = new JButton("4"); 
    		n5 = new JButton("5"); 
    		n6 = new JButton("6"); 
    		n7 = new JButton("7"); 
    		n8 = new JButton("8"); 
    		n9 = new JButton("9"); 
    		start = new JButton("START"); 
    		n0 = new JButton("0"); 
    		stop = new JButton("STOP"); 
     
    		North= new JPanel(new BorderLayout(260,0));
    		South= new JPanel(new FlowLayout(FlowLayout.RIGHT));
    		West = new JPanel(new FlowLayout(FlowLayout.LEFT));
    		East = new JPanel(new FlowLayout(FlowLayout.RIGHT));
     
     
    		keyButtons = new JPanel();
     
    		time = new JPanel();
    		screen = new JTextField("SCREEN");
     
     
     
     
    		West.add(screen);
    		time.add(timer);
     
    		North.add(time,BorderLayout.EAST);
    		South = new JPanel();
     
    	}
     
    		public void launchFrame(){
     
    			keyButtons.setLayout(new GridLayout(4,3));
     
    			South.setPreferredSize(new Dimension(100,100));
    			screen.setPreferredSize(new Dimension(440,700));
    			timer.setPreferredSize(new Dimension(227,70));
    			keyButtons.setPreferredSize(new Dimension(235,250));
    			screen.add(new JTextField(8));
    			//INPUT WILL START AT THE CENTER
    			 timer.setHorizontalAlignment(JTextField.CENTER);
    			 screen.setHorizontalAlignment(JTextField.CENTER);
    			 //COLOR FOR JTEXTFIELD BACKGROUND//
    			 timer.setBackground(Color.DARK_GRAY); 
    			 screen.setBackground(Color.DARK_GRAY); 
    			 //FONT COLOR //
    			 screen.setForeground(Color.RED);  
    			 timer.setForeground(Color.RED);
    			 //FONT SIZE//
    				screen.setFont(new Font("Tahoma",1,55));
    				timer.setFont(new Font("BOLD",1,35));
     
    			keyButtons.add(n1);
    			keyButtons.add(n2);
    			keyButtons.add(n3);
    			keyButtons.add(n4);
    			keyButtons.add(n5);
    			keyButtons.add(n6);
    			keyButtons.add(n7);
    			keyButtons.add(n8);
    			keyButtons.add(n9);
    			keyButtons.add(start);
    			keyButtons.add(n0);
    			keyButtons.add(stop);
     
     
     
     
     
    			n1.addActionListener(this);
    			n2.addActionListener(this);
    			n3.addActionListener(this);
    			n4.addActionListener(this);
    			n5.addActionListener(this);
    			n6.addActionListener(this);
    			n7.addActionListener(this);
    			n8.addActionListener(this);
    			n9.addActionListener(this);
    			n0.addActionListener(this);
    			start.addActionListener(this);
    			stop.addActionListener(this);
     
     
     
     
     
     
    			JFrame dos = new JFrame();
    			dos.setSize(700,500);
    			dos.add(keyButtons,BorderLayout.EAST);
     
    			dos.add(screen,BorderLayout.WEST);
     
    			dos.add(North,BorderLayout.NORTH);
    			dos.add(South,BorderLayout.SOUTH);
    			dos.setVisible(true);
    			dos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
     
    		}
     
     
    		public static void main (String args[]){
    			MicroWave GUI = new MicroWave();
    			GUI.launchFrame();
     
     
     
     
     
     
    		}
     
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Timer Action Listener

    The first code uses == vs the equals() method to compare Strings.

    it will start the countdown even if i click any jbuttons
    Is that wrong? What is it supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Action Listeners within and Action Listener on Panels
    By avidlearner in forum AWT / Java Swing
    Replies: 3
    Last Post: April 8th, 2014, 12:21 PM
  2. [SOLVED] Anonymous inner class with Action Listener and Abstract Action
    By shakes6791 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 13th, 2013, 03:52 PM
  3. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  4. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM