Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Java + Timer concept
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


vicky
Java + Timer concept
2 Answer(s)      5 years and 2 months ago
Posted in : Java Beginners



Advertisement
View Answers

August 29, 2009 at 10:33 AM


Hi Friend,

Try the following code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.html.*;

public class Test{
final static int interval = 1000;
int i;
JLabel label;
JProgressBar pb;
Timer timer;
JButton button;
public Test() {
JFrame frame = new JFrame("Swing Progress Bar");
button = new JButton("Start");
button.addActionListener(new ButtonListener());

pb = new JProgressBar(0, 20);
pb.setValue(0);
pb.setStringPainted(true);
label = new JLabel("Roseindia");
JPanel panel = new JPanel();
panel.add(button);
panel.add(pb);
JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(panel, BorderLayout.NORTH);
panel1.add(label, BorderLayout.CENTER);
panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
frame.setContentPane(panel1);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
timer = new Timer(interval, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (i == 20){
Toolkit.getDefaultToolkit().beep();
timer.stop();
button.setEnabled(true);
pb.setValue(0);
String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + "Completed." + "</b>" + "</font>" + "</html>";
label.setText(str);
}
i = i + 1;
pb.setValue(i);
}
});
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
button.setEnabled(false);
i = 0;
String str = "<html>" + "<font color=\"#008000\">" + "<b>" + "Work in progress......." + "</b>" + "</font>" + "</html>";
label.setText(str);
timer.start();
}
}

public static void main(String[] args) {
Test spb = new Test();
}
}

Hope that it will be helpful for you.
Thanks


March 6, 2012 at 7:30 PM


This is really helpful ! thank you so much for sharing the code :)



Related Tutorials/Questions & Answers:
Java + Timer concept - Java Beginners
Java + Timer concept  sir, I am using swing based model. My form... interval) and retrieve values and print it in console. I want code using timer concept.  Hi Friend, Try the following code: import java.awt.
Java timer
Java timer  Hi. I want to use a timer in one of my java projects... java.util.Timer; import java.util.TimerTask; public class ToDo { Timer timer; public ToDo ( int seconds ) { timer = new Timer
Advertisements
Java Timer Task
Java Timer Task  How to assign a task in java at specified date & time (with seconds) using threads
code for timer in java netbeans for online test series countdown timer
code for timer in java netbeans for online test series countdown timer  code for timer in java netbeans for online test series countdown timer
Timer in the code - Java Beginners
Timer in the code  This is the question I posted yesterday: "I'm trying to write a program that has a text field for input, two buttons and the output text area. A user is asked to type in a number. The first button when pressed
java hello and timer alert application
java hello and timer alert application  Hi, Please help me create a simple Java application that can display "Hello world" and current Time alert on mobile device screen using mobile screen emulator. Regards
Java Timer
Java Timer  Hai Sir, I want to know How to schedule a task on first date of every month in java by using Java timer.Please help me sir. thank you, Shyam
j2me timer and timer task
j2me timer and timer task  have anyone idea to change a direction of bubble, according to changing time.   Please go through the following links: http://www.roseindia.net/j2me/timer-midlet.shtml http
Difference between Timer and Thread?
Difference between Timer and Thread?  Can anyone tell me about the difference between Timer and Thread, Why we need to have Timer in case we have Thread implimentation startegy in Java
Timer and TimerTask
Timer and TimerTask  i have problem of java.lang.IllegalStateException: Task already scheduled or cancelled
Insertion Sort Timer
Insertion Sort Timer  Welcome all I wanna program in java find the timer of insertion sort and find time complexity for random value thanks all
timer using javascript
timer using javascript  How to display timer on HTML page using JavaScript
Timer - Swing AWT
Timer  How can I make a simple countdown timer in my GUI?  ... { ProgressMonitor progress; static int counter = 0; Timer timer...("Progress bar timer Demo"); setSize(250, 100); setDefaultCloseOperation
javascript timer countdown
javascript timer countdown  How to disable/enable the javascript timer countdown?   <!-- CSS Code --> #txt { border:none; font... = "yourpage.htm" // redirects to specified page once timer ends and ok button
Countdown timer to show a link
Countdown timer to show a link  Hi to everyone! I have this code, which is basically a countdown widget...what I want is that when the countdown finish, a link appears. The thing is that my musical group will be presenting an EP
JavaScript Clock Timer
JavaScript Clock Timer...; In this section, we are going to show you a clock timer using JavaScript... than 9, then it will add 0 in front of min, sec or hour value like 09 in the timer
ServletContextListener with Timer
In this section, you will learn how to use timer with ServletContextListener to run code snippet after every 1 minute immediate after the application run
Registration timer expired, but client is still registering!
Registration timer expired, but client is still registering!  Registration timer expired, but client is still registering
J2ME Timer MIDlet Example
J2ME Timer MIDlet Example       This Example shows how to use of timer class. In this example we are using the Timer class to create the time of execution  of application
Retrieval data from database against timer
Retrieval data from database against timer  Please send me a sample timer code which retrieve data from the database against timer in JSP
J2ME Timer Animation
J2ME Timer Animation       This application illustrates how to use the Timer class..., which helps you to understand using of timer class for drawing the canvas
Scheduling a Timer Task
Scheduling a Timer Task       In this section, you can learn how to schedule a timer task to run... the timer task at a certain time means the task will work only once while you
Please tell me how can i convert string to timer
Please tell me how can i convert string to timer  Please tell me how can i convert string to timer
stopwatch
stopwatch  Java code for stopwatch timer which should start from zero
Hacking
Hacking  hi friends.. can any one tell how to hack the ip address using java coding and i need to add timer in frame .. then i want to compare timer value ...pls help me asap.. i ll thank full
Comparison between the types of sorting in java
program in java compare between selection,insertion,bubble,merge,quick sort In terms of timer and put all types in frame and find the timer for example array 0-10000 the program select random value and it found timer for all types
java - Java Server Faces Questions
java  how to write the code for a schedular in java plz help in this i will be thank ful to u   hi friend, import java.util.*; public class SchedularTest { private final Timer timer = new Timer(); int
java - Java Beginners
it in console.By using timer concept or thread or some other concepts.  Hi
java - Java Beginners
it in console.I want code using timer concept
Jsp count and java bean - JSP-Servlet
Jsp count and java bean  Please am on a project and i want to create a countdown timer that will have this format HH:MM:SS. Am working with jsp and java beans please somebody help.  Hi Friend, Try the following
Advertisements
 

 

 

DMCA.com