EDIT: I have created a countdown timer, however I cannot figure out how to update without creating a new instance. The code is below:
import java.util.concurrent.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.DAYS;
public class Countdown {
static int countdownStarterD = 7;
static int countdownStarterH = 24;
static int countdownStarterM = 60;
static int countdownStarterS = 60;
String cSD = Integer.toString(countdownStarterD);
String cSH = Integer.toString(countdownStarterH);
String cSM = Integer.toString(countdownStarterM);
String cSS = Integer.toString(countdownStarterS);
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel days = new JLabel(cSD);
JLabel hours = new JLabel(cSH);
JLabel minutes = new JLabel(cSM);
JLabel seconds = new JLabel(cSS);
public Countdown() {
panel.add(days);
panel.add(hours);
panel.add(minutes);
panel.add(seconds);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Countdown Timer");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final Runnable days = new Runnable() {
public void run() {
System.out.println(countdownStarterD);
countdownStarterD--;
if (countdownStarterD < 0) {
countdownStarterD = 6;
}
}
};
final Runnable hours = new Runnable() {
public void run() {
System.out.println(countdownStarterH);
countdownStarterH--;
if (countdownStarterH < 0) {
countdownStarterH = 23;
}
}
};
final Runnable minutes = new Runnable() {
public void run() {
System.out.println(countdownStarterM);
countdownStarterM--;
if (countdownStarterM < 0) {
countdownStarterM = 59;
}
}
};
final Runnable seconds = new Runnable() {
public void run() {
System.out.println(countdownStarterS);
countdownStarterS--;
if (countdownStarterS < 0) {
countdownStarterS = 59;
}
}
};
final Runnable gui = new Runnable() {
public void run() {
new Countdown();
}
};
scheduler.scheduleAtFixedRate(days, 0, 1, DAYS);
scheduler.scheduleAtFixedRate(hours, 0, 1, HOURS);
scheduler.scheduleAtFixedRate(minutes, 0, 1, MINUTES);
scheduler.scheduleAtFixedRate(seconds, 0, 1, SECONDS);
scheduler.scheduleAtFixedRate(gui, 0, 1, SECONDS);
}
}
OLD: I am trying to create a simple GUI timer that counts down from 1 week and restarts once it is finished, it doesn't need to run anything just show a countdown. I would also like it so have a set time. I am stuck trying to convert the number of seconds in the countdown to Days:Hours:Minutes:Seconds and I don't know how to connect that conversion to a GUI. I am thinking I could possibly use a JLabel but I am not sure.
I have this code so far that I found on another forum.
import java.util.concurrent.*;
import static java.util.concurrent.TimeUnit.SECONDS;
public class Countdown {
public static void main(String[] args) {
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final Runnable runnable = new Runnable() {
int countdownStarter = 20;
public void run() {
System.out.println(countdownStarter);
countdownStarter--;
if (countdownStarter < 0) {
System.out.println("Timer Over!");
scheduler.shutdown();
}
}
};
scheduler.scheduleAtFixedRate(runnable, 0, 1, SECONDS);
}
}
It counts down from 20 at the moment which you can change by changing
int countdownStarter = 20;
I have noticed that there is a
import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
That can be used in the code I have so far. Any help on this would be greatly appreciated. I am not looking for a substitute application to use instead.