定期刷新数据

时间:2013-09-15 19:30:31

标签: javafx-2 javafx javafx-8

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

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

你能帮我正确实施这个例子吗?

编辑:第二次测试:

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

我也对此进行了测试,但数据未更新。

1 个答案:

答案 0 :(得分:4)

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组件。

如果您想使用ScheduledService,这是一个基本示例:

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

}
相关问题