From 2613f8a529f742ace489e758dbce8e4952f08a9d Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Sun, 15 Jan 2023 22:58:03 -0500 Subject: [PATCH] Fixed the periodic update --- src/main/java/net/metaunix/App.java | 5 ---- src/main/java/net/metaunix/SysInfo.java | 2 ++ .../metaunix/controllers/HomeController.java | 22 ++++++++------ .../services/UpdateSystemInfoService.java | 30 +++++++++++++++++++ .../net/metaunix/tasks/UpdateSystemTask.java | 16 ---------- 5 files changed, 45 insertions(+), 30 deletions(-) create mode 100644 src/main/java/net/metaunix/services/UpdateSystemInfoService.java delete mode 100644 src/main/java/net/metaunix/tasks/UpdateSystemTask.java diff --git a/src/main/java/net/metaunix/App.java b/src/main/java/net/metaunix/App.java index 94d13c1..d25c854 100644 --- a/src/main/java/net/metaunix/App.java +++ b/src/main/java/net/metaunix/App.java @@ -37,11 +37,6 @@ public class App extends Application { stage.show(); } - @Override - public void stop() { - HomeController.ust.cancel(); - } - public static void main(String[] args) { launch(); } diff --git a/src/main/java/net/metaunix/SysInfo.java b/src/main/java/net/metaunix/SysInfo.java index d785d64..3a265dc 100644 --- a/src/main/java/net/metaunix/SysInfo.java +++ b/src/main/java/net/metaunix/SysInfo.java @@ -2,6 +2,8 @@ package net.metaunix; public class SysInfo { + public static String osString = "N/a"; + public static String javaVersion() { return System.getProperty("java.version"); } diff --git a/src/main/java/net/metaunix/controllers/HomeController.java b/src/main/java/net/metaunix/controllers/HomeController.java index b6f275b..aef68cc 100644 --- a/src/main/java/net/metaunix/controllers/HomeController.java +++ b/src/main/java/net/metaunix/controllers/HomeController.java @@ -2,25 +2,29 @@ package net.metaunix.controllers; import javafx.fxml.FXML; import javafx.scene.control.Label; -import oshi.SystemInfo; -import oshi.software.os.OperatingSystem; +import javafx.util.Duration; -import net.metaunix.tasks.UpdateSystemTask; + +import net.metaunix.SysInfo; +import net.metaunix.services.UpdateSystemInfoService; public class HomeController { - public static UpdateSystemTask ust = new UpdateSystemTask(); - @FXML private Label osInfo; @FXML public void initialize() { - SystemInfo si = new SystemInfo(); - OperatingSystem os = si.getOperatingSystem(); - osInfo.setText(String.valueOf(os)); + UpdateSystemInfoService usis = new UpdateSystemInfoService(); + usis.setOnSucceeded((e) -> { + updateLabels(); + }); + usis.setPeriod(Duration.seconds(3)); + usis.start(); + } - new Thread(this.ust).start(); + private void updateLabels() { + osInfo.setText(SysInfo.osString); } } diff --git a/src/main/java/net/metaunix/services/UpdateSystemInfoService.java b/src/main/java/net/metaunix/services/UpdateSystemInfoService.java new file mode 100644 index 0000000..4fc31af --- /dev/null +++ b/src/main/java/net/metaunix/services/UpdateSystemInfoService.java @@ -0,0 +1,30 @@ +package net.metaunix.services; + +import javafx.concurrent.ScheduledService; +import javafx.concurrent.Task; +import oshi.SystemInfo; +import oshi.software.os.OperatingSystem; + +import net.metaunix.SysInfo; + +public class UpdateSystemInfoService extends ScheduledService { + + private static int counter = 0; + + @Override + protected Task createTask() { + return new Task<>() { + + @Override + protected Void call() throws Exception { + UpdateSystemInfoService.counter++; + SystemInfo si = new SystemInfo(); + OperatingSystem os = si.getOperatingSystem(); + SysInfo.osString = String.valueOf(UpdateSystemInfoService.counter) + " " + String.valueOf(os); + return null; + } + + }; + } + +} diff --git a/src/main/java/net/metaunix/tasks/UpdateSystemTask.java b/src/main/java/net/metaunix/tasks/UpdateSystemTask.java deleted file mode 100644 index ec15827..0000000 --- a/src/main/java/net/metaunix/tasks/UpdateSystemTask.java +++ /dev/null @@ -1,16 +0,0 @@ -package net.metaunix.tasks; - -import java.lang.Thread; -import javafx.concurrent.Task; - -public class UpdateSystemTask extends Task { - - @Override - protected Void call() throws Exception { - while (true) { - System.out.println("Test"); - Thread.sleep(2000); - } - } - -}