i have a question , i have this timer code in java that when its executed it displays a count down timer on its own JFrame label, what i want to do is to display this timer on another JFrame form label without having to move the code to other classes.
I hope you can help me with this thanks lot guys .
this is the code for the Timer class:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class TimerExample extends JFrame {
final JLabel label;
Timer countdownTimer;
int timeRemaining = 10;
public TimerExample() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200, 200);
label = new JLabel(String.valueOf(timeRemaining), JLabel.CENTER);
getContentPane().add(label);
countdownTimer = new Timer(1000, new CountdownTimerListener());
setVisible(true);
countdownTimer.start();
}
class CountdownTimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (--timeRemaining > 0) {
label.setText(String.valueOf(timeRemaining));
} else {
label.setText("Time's up!");
countdownTimer.stop();
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new TimerExample();
}
});
}
}
thanks
Here it is,
Following is my TestTimer class which accepts a JLabel as input
And here is another Main class which is actually extending a JFrame and showing a label in it,
Second Code passes a created JLabel to first class and first class uses it to show timer.