USING TIMERS WITH SWING.
USING TIMERS WITH SWING.
A timer instance can be considered as a unique thread that fires one or more action events at specified intervals. The timer class for use in GUI contexts in java is defined in the javax.swing.Timer class.
Let's write a code for creating a Timer class.
package tests;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Property extends JPanel{
static JButton jButton1 = new JButton(" ");
static JTextArea jText;
/** Creates a new instance of Property */
public Property() {
jButton1 = new JButton("click me");
add(jButton1);
jText = new JTextArea("welcome", 2, 50);
add(jText);
//start the timer
int delay = 10000; // 3 sec. delay initially and between firing
//the action listener that processes the timer event
ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent ae){
simplyText("Don't forget me.");
}
};
//create an instance of a timer with delay and action listener
//then start timer thread
Timer tim = new Timer (delay, al);
tim.start();
}
private static void dButton(){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Property");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Property nProp = new Property();
nProp.setOpaque(true);
frame.setContentPane(nProp);
frame.pack();
frame.setVisible(true);
}
public static void simplyText(String txt){
jText.setText(txt);
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
dButton();
}
});
}
}

No comments:
Post a Comment