How to Make time textfield Runnable
class chap2 extends JFrame implements ActionListener,Runnable{
Thread runner;
JLabel lab1=new JLabel("Time ");
JTextField tf1=new JTextField(20);
chap2(){
start();repaint();
setLayout(new FlowLayout());
tf1.setEditable(false);
add(lab1);
add(tf1);
}
void get_Time(){
Date date = new Date();
//DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("GMT+5"));
//df.setTimeZone(TimeZone.getDefault());
String Timex=("" + df.format(date));
df.setTimeZone(TimeZone.getDefault());
tf1.setText(""+Timex);
}
@Override
public void run() {
while (true) {
get_Time();
repaint();
try { Thread.sleep(1000); }
catch (InterruptedException e) { }
}
}
public void start() { if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void stop() {
if (runner != null) {
runner.stop();
runner = null;
} }
public void paint() {
get_Time();
}
}
No comments:
Post a Comment