JavaFx文本字段未在GUI上更新 [英] JavaFx Text fields are not updating on GUI

查看:126
本文介绍了JavaFx文本字段未在GUI上更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对内存可视化器进行编码,该内存可视化器将使用当前内存统计信息更新图形,并在其侧面具有数字表示。我有一个在线程中运行的更新方法,应该每秒更新一次。

I am trying to code a memory visualizer that will update a graph with current memory statistics and have a numerical representation just to the side of it. I have an update method that runs in a thread and should update every second.

问题是它没有在GUI上更新,事实上我相信代码在一次迭代后停止。我相信它与一些setText()调用有关,因为如果我将它们注释掉,那么至少图表会更新。

The issue is that none of it updates on the GUI, and in fact I believe the code stops after one iteration. I believe it has to do with some of the setText() calls because if I comment them out, then at least the graph will update.

这是我的初始化方法

@Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        assert memUsage_lineChart != null : "fx:id=\"memUsage_lineChart\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert text_totalMem != null : "fx:id=\"text_totalMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert text_usedMem != null : "fx:id=\"text_usedMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert text_availableMem != null : "fx:id=\"text_availableMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert text_percentageUsed != null : "fx:id=\"text_percentageUsed\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert text_totalRuntime != null : "fx:id=\"text_totalRuntime\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert text_threadsUsed != null : "fx:id=\"text_threadsUsed\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert text_version != null : "fx:id=\"text_version\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert text_timeAtMaxMem != null : "fx:id=\"text_timeAtMaxMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert text_maxMemReached != null : "fx:id=\"text_maxMemReached\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert text_timeOfLastAssociation != null : "fx:id=\"text_timeOfLastAssociation\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert text_something != null : "fx:id=\"text_totalRuntime\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert lineChart_yAxis != null : "fx:id=\"lineChart_yAxis\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
        assert lineChart_xAxis != null : "fx:id=\"lineChart_xAxis\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";

        runtime = 0L;


        //Initializing the ArrayLists<> that will contain the series of data for each possible graph
        memData = new ArrayList<XYChart.Series<Number,Number>>();       

        //Setting the Y-axis of the chart to change according to incoming data.
        memUsage_lineChart.getYAxis().setAutoRanging(true);

        //Initializes the data arrays and series for the charts 
        XYChart.Series<Number, Number> series_sec = new XYChart.Series<Number,Number>();
        memUsage_lineChart.getData().add(series_sec);
        memData.add(series_sec);

        //Creates a thread that continuously runs and adds data points to the displays
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
        ses.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                runtime += 1000;
                updateData();
                try {
                    Thread.sleep(500);
                    } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, 0, 500, TimeUnit.MILLISECONDS);

    }/*END OF INSTANTIATION*/

这是我的updateData ()方法

And here is my updateData() method

public void updateData()
{
    //Gathers the runtime memory data from the system
    long maxFreeMemory = Runtime.getRuntime().maxMemory();
    long usableFreeMemory= Runtime.getRuntime().maxMemory()
            -Runtime.getRuntime().totalMemory()
            +Runtime.getRuntime().freeMemory();
    double usedPercent=(double)(Runtime.getRuntime().totalMemory()
            -Runtime.getRuntime().freeMemory())/Runtime.getRuntime().maxMemory();
    long usedMemory = (long)(usedPercent*maxFreeMemory);
    maxFreeMemory /= 1000000;
    usableFreeMemory /= 1000000;
    usedMemory /= 1000000;
    usedPercent *= 100;
    DecimalFormat df = new DecimalFormat("0.00");

    //Set the text values for the runtime statistics
    text_totalMem.textProperty().set(String.valueOf(maxFreeMemory));
    text_usedMem.textProperty().set(String.valueOf(usedMemory));
    text_availableMem.textProperty().set(String.valueOf(usableFreeMemory));
    text_percentageUsed.textProperty().set(df.format(usedPercent)+" %");
    text_totalRuntime.textProperty().set((runtime/3600000)+":"+String.format("%02d",((runtime/60000)%60))+":"+String.format("%02d",((runtime%60000)/1000)));
    text_threadsUsed.textProperty().set(String.valueOf(ManagementFactory.getThreadMXBean().getThreadCount()));

    //Updates the axes of the real-time graphs/charts
    if(runtime >= 120000 && (runtime % 12000) == 0)
    {
        lineChart_xAxis.setLowerBound(lineChart_xAxis.getLowerBound()+12);
        lineChart_xAxis.setUpperBound(lineChart_xAxis.getUpperBound()+12);

        for(int i=0; i<12; i++)
            memData.get(0).getData().remove(0);
    }

    //Adds the data to the series to be placed on to the graphs/charts
    memData.get(0).getData().add(new XYChart.Data<Number,Number>(runtime/1000,usedMemory));
}

有没有理由我的GUI卡住并且不会更新文本字段/图表什么时候运行?我知道一个事实是线程至少运行一次迭代,因为当GUI弹出时文本字段不是0(初始化)。

Is there a reason my GUI gets stuck and wont update the text fields/graph when this is run? I know for a fact that the thread runs at least one iteration since the text fields are not 0 (as initialized) when the GUI pops up.

推荐答案

使用 Platform.runLater()在FX应用程序线程上执行 updateData()方法。 (此外,我认为 Thread.sleep(...)是多余的,因为您使用的是预定的执行程序。)

Execute the updateData() method on the FX Application Thread using Platform.runLater(). (Additionally, I think the Thread.sleep(...) is redundant, since you are using a scheduled executor.)

    ses.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            runtime += 1000;
            Platform.runLater(() -> updateData());
        }
    }, 0, 500, TimeUnit.MILLISECONDS);

这篇关于JavaFx文本字段未在GUI上更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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