Results 1 to 6 of 6
- 06-28-2009, 06:11 PM #1
Member
- Join Date
- Nov 2008
- Location
- Colorado
- Posts
- 18
- Rep Power
- 0
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.
My problem is to stop the timer and turn the background of button one back to the default color.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
-
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 07:25 PM.
-
You seem to be doing the opposite in the timer's ActionListener -- setting the background color to red, and this is a bit curious.
Also, why is everything static?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 };
- 06-28-2009, 07:41 PM #4
Member
- Join Date
- Nov 2008
- Location
- Colorado
- Posts
- 18
- Rep Power
- 0
Fubarable
Thank you for the excellent help. This is my first timer.
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.Also, why is everything static?
-
You're quite welcome.
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.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.
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(); } }); } }
- 06-28-2009, 08:33 PM #6
Member
- Join Date
- Nov 2008
- Location
- Colorado
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
to get count value as a variable
By arunkumarinfo in forum JDBCReplies: 2Last Post: 03-30-2009, 01:32 AM -
Select Count
By Apple2 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 04-29-2008, 09:02 AM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM -
Getting row count
By Java Tip in forum Java TipReplies: 0Last Post: 02-11-2008, 08:49 AM -
making a count down timer using java
By saytri in forum New To JavaReplies: 3Last Post: 12-29-2007, 09:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks