I want to grab the current date and time from the system which i can do with this code:
private void GetCurrentDateTimeActionPerformed(java.awt.event.ActionEvent evt) {
DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
CurrentDateTime.setText(dateandtime.format(date));
}
Doing this is fine as it will grab he current date nad time no problem, however it is not dynamic as the time will not update unless the button is pressed again. So I was wondering how I could make this button more dynamic by updating the function every second to refresh the time.
A Swing timer (an instance of javax.swing.Timer) fires one or more action events after a specified delay. Refer: http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html
This answer might be useful for you
You can use an executor to update that periodically. Something like this:
Use Swing Timer for this :
First define a TimerTask
Then, you need to create a java.util.Timer in startup of your application or UI. For example your main() method.