Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-28-2009, 07:11 PM
Member
 
Join Date: Nov 2008
Location: Colorado
Posts: 18
Rep Power: 0
caryr is on a distinguished road
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.
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-28-2009, 08:21 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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):

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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-28-2009, 08:30 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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.
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-28-2009, 08:41 PM
Member
 
Join Date: Nov 2008
Location: Colorado
Posts: 18
Rep Power: 0
caryr is on a distinguished road
Default
Fubarable
Thank you for the excellent help. This is my first timer.
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-28-2009, 09:19 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
Originally Posted by caryr View Post
Fubarable
Thank you for the excellent help. This is my first timer.
You're quite welcome.

Quote:
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):

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();
      }
    });
  }
}
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-28-2009, 09:33 PM
Member
 
Join Date: Nov 2008
Location: Colorado
Posts: 18
Rep Power: 0
caryr is on a distinguished road
Default
Thank you so much for all the great information. Now I know why it was wanting static for everything.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
to get count value as a variable arunkumarinfo Database 2 03-30-2009 02:32 AM
Select Count Apple2 JavaServer Pages (JSP) and JSTL 1 04-29-2008 10:02 AM
How to cancel an individual timer in spite of canceling whole timer Java Tip java.util 0 04-04-2008 03:46 PM
Getting row count Java Tip Java Tips 0 02-11-2008 09:49 AM
making a count down timer using java saytri New To Java 3 12-29-2007 10:49 PM


All times are GMT +2. The time now is 06:28 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org