Java + Timer concept

Ads

 
 

Share on Google+Share on Google+

vicky
Java + Timer concept
2 Answer(s)      8 years ago
Posted in : Java Beginners
Ads
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 :)


Ads









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 - 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
timer using javascript
timer using javascript  How to display timer on HTML page using JavaScript
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
Version of geronimo>geronimo-timer dependency
List of Version of geronimo>geronimo-timer dependency
Version of com.ineunet>knife-timer dependency
List of Version of com.ineunet>knife-timer dependency
Version of com.rhcloud>timemanager-timer dependency
List of Version of com.rhcloud>timemanager-timer dependency
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
Registration timer expired, but client is still registering!
Registration timer expired, but client is still registering!  Registration timer expired, but client is still registering
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
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
Version of com.spikhalskiy>hashed-wheel-timer dependency
List of Version of com.spikhalskiy>hashed-wheel-timer dependency
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
Maven Dependency geronimo-timer >> 1.0-M2
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.0-M2 in your project
Maven Dependency geronimo-timer >> 1.0-M5
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.0-M5 in your project
Maven Dependency timemanager-timer >> 1.6-RELEASE
You should include the dependency code given in this page to add Maven Dependency of com.rhcloud >> timemanager-timer version1.6-RELEASE in your project
Maven Dependency geronimo-timer >> 1.0
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.0 in your project
Maven Dependency geronimo-timer >> 1.0-M3
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.0-M3 in your project
Maven Dependency geronimo-timer >> 1.0-M4
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.0-M4 in your project
Maven Dependency geronimo-timer >> 1.1
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.1 in your project

Ads

 
Advertisement null

Ads