Periodically refresh data

孤街醉人 提交于 2020-01-24 12:03:25

问题


I want to display data periodically but I get error when I try to display the data.

Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(3), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {

        GridPane gp = new GridPane();

        gp.setPadding(new Insets(10, 10, 10, 10));
        gp.setHgap(20);
        gp.setVgap(10);
        //gp.setGridLinesVisible(true);

        gp.add(new Label("Operating System Name"), 0, 0);
        gp.add(new Label(System.getProperty("os.name")), 1, 0);

        gp.add(new Label("Operating System Version"), 0, 1);        
        gp.add(new Label(System.getProperty("os.version")), 1, 1);

        gp.add(new Label("System Architecture"), 0, 2);        
        gp.add(new Label(System.getProperty("os.arch")), 1, 2);

        gp.add(new Label("Total Memory"), 0, 3);
        String memStrLong = Long.toString(Runtime.getRuntime().totalMemory());
        gp.add(new Label(memStrLong), 1, 3);

        gp.add(new Label("Used Memory"), 0, 4);
        String fStrLong = Long.toString(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
        gp.add(new Label(fStrLong), 1, 4);

        gp.add(new Label("Java Version"), 0, 5);        
        gp.add(new Label(System.getProperty("java.version")), 1, 5);

        gp.add(new Label("Number of Processors"), 0, 6);       
        String pStrLong = Long.toString(Runtime.getRuntime().availableProcessors());
        gp.add(new Label(pStrLong), 1, 6);

        gp.add(new Label("Maximum available Memory"), 0, 7);       
        String amStrLong = Long.toString(Runtime.getRuntime().maxMemory());
        gp.add(new Label(amStrLong), 1, 7);

    }
}));
fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);
fiveSecondsWonder.play();

Vbox.getChildren().add(gp);

Can you help me to implement properly this example, please?

EDIT: Second test:

ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {  
            @Override  
            public Thread newThread(Runnable r) {  
                Thread thread = new Thread(r);  
                thread.setDaemon(true);  
                return thread;  
            }  
        });  
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {  

            final GridPane gp = new GridPane();

            @Override  
            public void run() {  

                    gp.getChildren().clear();

                    gp.setPadding(new Insets(10, 10, 10, 10));
                    gp.setHgap(20);
                    gp.setVgap(10);
                    //gp.setGridLinesVisible(true);

                    gp.add(new Label("Operating System Name"), 0, 0);
                    gp.add(new Label(System.getProperty("os.name")), 1, 0);

                    gp.add(new Label("Operating System Version"), 0, 1);        
                    gp.add(new Label(System.getProperty("os.version")), 1, 1);

                    gp.add(new Label("System Architecture"), 0, 2);        
                    gp.add(new Label(System.getProperty("os.arch")), 1, 2);

                    gp.add(new Label("Total Memory"), 0, 3);
                    String memStrLong = Long.toString(Runtime.getRuntime().totalMemory());
                    gp.add(new Label(memStrLong), 1, 3);

                    gp.add(new Label("Used Memory"), 0, 4);
                    String fStrLong = Long.toString(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
                    gp.add(new Label(fStrLong), 1, 4);

                    gp.add(new Label("Java Version"), 0, 5);        
                    gp.add(new Label(System.getProperty("java.version")), 1, 5);

                    gp.add(new Label("Number of Processors"), 0, 6);       
                    String pStrLong = Long.toString(Runtime.getRuntime().availableProcessors());
                    gp.add(new Label(pStrLong), 1, 6);

                    gp.add(new Label("Maximum available Memory"), 0, 7);       
                    String amStrLong = Long.toString(Runtime.getRuntime().maxMemory());
                    gp.add(new Label(amStrLong), 1, 7);

                    vb.getChildren().add(gp);

            }  
        }, 0, 1, TimeUnit.SECONDS);

I also tested this but with the data is not updated.


回答1:


public class Sandbox extends Application {

private final StringProperty totalMemoryProperty = new SimpleStringProperty();
private final StringProperty usedMemoryProperty = new SimpleStringProperty();
private final StringProperty maxMemoryProperty = new SimpleStringProperty();

public static void main(final String[] args) throws Exception {
    launch(args);
}

@Override
public void start(final Stage stage) throws Exception {
    final GridPane gp = new GridPane();
    gp.setPadding(new Insets(10, 10, 10, 10));
    gp.setHgap(20);
    gp.setVgap(10);
    gp.add(new Label("Operating System Name"), 0, 0);
    gp.add(new Label(System.getProperty("os.name")), 1, 0);
    gp.add(new Label("Operating System Version"), 0, 1);
    gp.add(new Label(System.getProperty("os.version")), 1, 1);
    gp.add(new Label("System Architecture"), 0, 2);
    gp.add(new Label(System.getProperty("os.arch")), 1, 2);
    gp.add(new Label("Java Version"), 0, 5);
    gp.add(new Label(System.getProperty("java.version")), 1, 5);
    gp.add(new Label("Number of Processors"), 0, 6);
    String pStrLong = Long.toString(Runtime.getRuntime().availableProcessors());
    gp.add(new Label(pStrLong), 1, 6);
    gp.add(new Label("Total Memory"), 0, 3);
    gp.add(new Label("Used Memory"), 0, 4);
    gp.add(new Label("Maximum available Memory"), 0, 7);
    final Label totalMemoryLabel = new Label();
    totalMemoryLabel.textProperty().bind(totalMemoryProperty);
    gp.add(totalMemoryLabel, 1, 3);
    final Label usedMemoryLabel = new Label();
    usedMemoryLabel.textProperty().bind(usedMemoryProperty);
    gp.add(usedMemoryLabel, 1, 4);
    final Label maxMemoryLabel = new Label();
    maxMemoryLabel.textProperty().bind(maxMemoryProperty);
    gp.add(maxMemoryLabel, 1, 7);

    final VBox rootNode = new VBox();
    rootNode.getChildren().add(gp);

    ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setDaemon(true);
            return thread;
        }
    });
    scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            final Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    String memStrLong = Long.toString(Runtime.getRuntime().totalMemory());
                    totalMemoryProperty.setValue(memStrLong);

                    String fStrLong = Long.toString(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
                    usedMemoryProperty.setValue(fStrLong);

                    String amStrLong = Long.toString(Runtime.getRuntime().maxMemory());
                    maxMemoryProperty.setValue(amStrLong);
                }
            };
            Platform.runLater(runnable);
        }
    }, 0, 1, TimeUnit.SECONDS);

    final Scene scene = new Scene(rootNode, 1024, 768);
    stage.setScene(scene);
    stage.show();
}

}

I made your code work with databinding, use Platform.runLater() to go back to the GUI thread to update javafx components.

If you want to use ScheduledService here is a basic example:

public class Sandbox extends Application {

public static void main(final String[] args) throws Exception {
    launch(args);
}

class MyService extends ScheduledService<List<String>> {

    @Override
    protected Task<List<String>> createTask() {
        final Task<List<String>> voidTask = new Task<List<String>>() {
            @Override
            protected List<String> call() throws Exception {
                List<String> results = new ArrayList<>();
                final String memStrLong = Long.toString(Runtime.getRuntime().totalMemory());
                results.add(memStrLong);
                final String fStrLong = Long.toString(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
                results.add(fStrLong);
                final String amStrLong = Long.toString(Runtime.getRuntime().maxMemory());
                results.add(amStrLong);
                return results;
            }
        };
        return voidTask;
    }
}

@Override
public void start(final Stage stage) throws Exception {
    final GridPane gp = new GridPane();
    gp.setPadding(new Insets(10, 10, 10, 10));
    gp.setHgap(20);
    gp.setVgap(10);
    gp.add(new Label("Operating System Name"), 0, 0);
    gp.add(new Label(System.getProperty("os.name")), 1, 0);
    gp.add(new Label("Operating System Version"), 0, 1);
    gp.add(new Label(System.getProperty("os.version")), 1, 1);
    gp.add(new Label("System Architecture"), 0, 2);
    gp.add(new Label(System.getProperty("os.arch")), 1, 2);
    gp.add(new Label("Java Version"), 0, 5);
    gp.add(new Label(System.getProperty("java.version")), 1, 5);
    gp.add(new Label("Number of Processors"), 0, 6);
    final String pStrLong = Long.toString(Runtime.getRuntime().availableProcessors());
    gp.add(new Label(pStrLong), 1, 6);
    gp.add(new Label("Total Memory"), 0, 3);
    gp.add(new Label("Used Memory"), 0, 4);
    gp.add(new Label("Maximum available Memory"), 0, 7);
    final Label totalMemoryLabel = new Label();
    gp.add(totalMemoryLabel, 1, 3);
    final Label usedMemoryLabel = new Label();
    gp.add(usedMemoryLabel, 1, 4);
    final Label maxMemoryLabel = new Label();
    gp.add(maxMemoryLabel, 1, 7);

    final VBox rootNode = new VBox();
    rootNode.getChildren().add(gp);

    final MyService service = new MyService();
    service.setDelay(new Duration(300));
    service.setPeriod(new Duration(1000));
    service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(final WorkerStateEvent workerStateEvent) {
            List<String> results = (List<String>) workerStateEvent.getSource().getValue();
            totalMemoryLabel.setText(results.get(0));
            usedMemoryLabel.setText(results.get(1));
            maxMemoryLabel.setText(results.get(2));
        }
    });
    service.start();

    final Scene scene = new Scene(rootNode, 1024, 768);
    stage.setScene(scene);
    stage.show();
}

}


来源:https://stackoverflow.com/questions/18816603/periodically-refresh-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!