I have this simple GUI for a time registration application
As the GUI is used for time registration, I'd like it to show a timer that runs as you press "Start" until you press "Stop".
Currently the two methods are done like this:
private void trStartBtnActionPerformed(java.awt.event.ActionEvent evt) {
trStartBtn.setEnabled(false);
trStopBtn.setEnabled(true);
startTime = System.nanoTime();
}
private void trStopBtnActionPerformed(java.awt.event.ActionEvent evt) {
endTime = System.nanoTime();
trStartBtn.setEnabled(true);
trStopBtn.setEnabled(false);
// Opgave Navn
String taskName = JOptionPane.showInputDialog(
this,
"Task name",
"Time Registration",
JOptionPane.INFORMATION_MESSAGE);
// Tiden det tog
long result = endTime - startTime;
double seconds = (double) result / 1000000000;
int minutes = (int)seconds / 60;
int hours = minutes / 60;
String time = hours + ";" + minutes + ";" + (int) seconds;
// Dags Dato
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date today = new Date();
String date = format.format(today);
DefaultTableModel m = (DefaultTableModel)trTable.getModel();
Object[] data = {date,time,taskName};
m.addRow(data);
trScrollPane.repaint();
}
But how would I add functionality so that the timer up to the right will tick every second as to show the time as you use the program? It's been a while and I am a bit rusty at Swing. I know that making a thread to take care of it is not safe.