Results 1 to 6 of 6
  1. #1
    caryr is offline Member
    Join Date
    Nov 2008
    Location
    Colorado
    Posts
    18
    Rep Power
    0

    Default How to use a timer to count up or down.

    Okay So i want to change the background of one button from default to red. When the button is pressed. Then after 1 minute I want to turn the color back to default. While the first button is red I still want to be able to use the second button and change its text by pressing it while button one is still read.
    This is what I have so far.
    Java Code:
    iimport java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class CoutDownDemo
    {
    	static JFrame setup;
    	static JButton one;
    	static JButton two;
    	static int seconds = 0;
    	 private static void createAndShowGUI() 
    	 {
    		 setup = new JFrame("Roller Derby Penalty Setup");
    	     setup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	     JPanel Home = new JPanel();
    	     one = new JButton("0");
    	     two = new JButton("0");
    	     int countOne = 0;
    	     int countTwo = 0;
    	     Home.add(one);
    	     Home.add(two);
    	     setup.add(Home);
            setup.setLocation(250,50);
            setup.pack();
            setup.setVisible(true);
            one.addActionListener(
    	      new ActionListener()
    	      {
    	        public void actionPerformed(ActionEvent e)
    	        {
    	        	System.out.println("Button one Top");
    	        	String temp = one.getText();
    	        	int numOne =  Integer.parseInt(temp);
    	        	numOne++;
    	        	temp = Integer.toString(numOne);
    	        	one.setText(temp);
    	        	one.setBackground(Color.red);
    	        	seconds = 0;
    	        	 int delay = 1000; //milliseconds
    	        	 Timer timer = new Timer(delay, taskPerformer);
    	        	timer.start();   
    	        	one.setBackground(null);
    	        }  // end actionPreformed
    	      }  // end ActionListener
    	     ); // end addActionListener one
            two.addActionListener(
          	      new ActionListener()
          	      {
          	        public void actionPerformed(ActionEvent e)
          	        {
          	        	System.out.println("Button two Top");
          	        	String temp = two.getText();
          	        	int numTwo =  Integer.parseInt(temp);
          	        	numTwo++;
          	        	temp = Integer.toString(numTwo);
          	        	two.setText(temp);     
          	        }  // end actionPreformed
          	      }  // end ActionListener
          	     ); // end addActionListener one
    	 }
        public static void main(String[] args)
        {
              //Schedule a job for the event-dispatching thread:
              //creating and showing this application's GUI.
              javax.swing.SwingUtilities.invokeLater(new Runnable() 
              {
                  public void run() 
                  {
                      createAndShowGUI();
                  } // end run
              } // end java.swing
            );// end java.swing
        } // end main
     	  static ActionListener taskPerformer = new ActionListener() 
     	  {
    	      public void actionPerformed(ActionEvent evt) 
    	      {
    	    	one.setBackground(Color.red);
    	    	System.out.println("Here" + seconds);
    	    	seconds ++;
    	    	if(seconds == 10)
    	    	{	
    	    	}
    	      }	// end actionPerformed
    	  };	// end ActionListener taskPerformer
    }	// end class CoutDownDemo
    My problem is to stop the timer and turn the background of button one back to the default color.

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

    Default

    The javax.swing.Timer API has a method that prevents it from looping more than once. I can't remember the name of the method (something like setRepeating(false)), but if you look it up, you'll find it.

    To set a button back to it's normal background, I believe that setBackground(null) may work. I'm not at my computer with a java compiler but rather am at work, so I can't test this. This needs to be called from within the Timer ActionListener's actionPerformed method.

    edit: the Timer method is setRepeats(false):

    Java Code:
    one.setBackground(Color.red);
    seconds = 0;
    int delay = 1000; //milliseconds
    Timer timer = new Timer(delay, taskPerformer);
    timer.setRepeats(false);
    timer.start();   
    // one.setBackground(null); this must be called in actionPerformed
    Last edited by Fubarable; 06-28-2009 at 08:25 PM.

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

    Default

    You seem to be doing the opposite in the timer's ActionListener -- setting the background color to red, and this is a bit curious.
    Java Code:
    // static???
    static ActionListener taskPerformer = new ActionListener() 
    {
       public void actionPerformed(ActionEvent evt) 
       {
         //one.setBackground(Color.red);
         one.setBackground(null);
         System.out.println("Here" + seconds);
       }	// end actionPerformed
    };
    Also, why is everything static?

  4. #4
    caryr is offline Member
    Join Date
    Nov 2008
    Location
    Colorado
    Posts
    18
    Rep Power
    0

    Default

    Fubarable
    Thank you for the excellent help. This is my first timer.
    Also, why is everything static?
    This is way Eclipse wanted. It was giving me errors without making it static. I think its do to the static main function. However I could be wrong.

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

    Default

    Quote Originally Posted by caryr View Post
    Fubarable
    Thank you for the excellent help. This is my first timer.
    You're quite welcome.

    This is way Eclipse wanted. It was giving me errors without making it static. I think its do to the static main function. However I could be wrong.
    It's not that Eclipse wants it this way, it's that you're creating a Swing app in a very non-OOP way, without creating objects.

    You may wish to keep your createAndShowGUI method as static, but use it to create and display a CountDownDemo object. A skeleton of a Swing app could look something like this (though as I'm not a professional, I'm not saying that this is "The Way" to absolutely do this):

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    import javax.swing.*;
    
    public class MySwingClass {
    
      // create main JPanel to hold app
      private JPanel mainPanel = new JPanel();
    
      public MySwingClass() {
        // add components to my mainPanel
      }
    
      // public method to export mainPanel
      // so it can be placed into a JFrame  
      public JPanel getMainPanel() {
        return mainPanel;
      }
      
      // static method to create a JFrame initialize my Swing class object
      // and place that object's JPanel int the JFrame
      private static void createAndShowUI() {
        // create the JFrame
        JFrame frame = new JFrame("Swing Demo");
    
        // create an instance of my Swing class:
        MySwingClass mySwingInstance = new MySwingClass();
    
        // add instance's main panel to the JFrame's contentPane
        frame.getContentPane().add(mySwingInstance.getMainPanel());
    
        // close app when JFrame exits
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        // tell layout managers to do their thing
        frame.pack(); 
    
        // center the app
        frame.setLocationRelativeTo(null);
    
        // and display it.
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
    
        // To call my Swing app in a thread-safe way
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }

  6. #6
    caryr is offline Member
    Join Date
    Nov 2008
    Location
    Colorado
    Posts
    18
    Rep Power
    0

    Default

    Thank you so much for all the great information. Now I know why it was wanting static for everything.

Similar Threads

  1. to get count value as a variable
    By arunkumarinfo in forum JDBC
    Replies: 2
    Last Post: 03-30-2009, 02:32 AM
  2. Select Count
    By Apple2 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-29-2008, 10:02 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:46 PM
  4. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 09:49 AM
  5. making a count down timer using java
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-29-2007, 10:49 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
  •