定期刷新数据 [英] Periodically refresh data

查看:125
本文介绍了定期刷新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定期显示数据,但是当我尝试显示数据时出现错误。

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?

编辑:第二次测试:

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.

推荐答案

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();
}

}

我使用数据绑定工作,使用Platform.runLater()返回GUI线程来更新javafx组件。

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

如果你想使用ScheduledService,这是一个基本的例子:

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();
}

}

这篇关于定期刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆