JavaFX TableView未更新 [英] JavaFX TableView is not updating

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

问题描述

我试图在 JavaFX TableView 中显示每个任务的进度和状态。每个任务一行代表 TableView

I am trying to show the progress and the status of the each task in the JavaFX TableView. Each task represents by a row into a TableView.

每个任务都在一个单独的线程中执行。任务可能是 - 从服务器下载文件,将文件从一个帐户复制到另一个帐户等。线程可能需要很长时间才能完成任务。我的应用程序还控制在队列中执行操作的线程数。使用默认设置,它一次最多可以运行5个线程。

Each task executes in a separate Thread. Task may be like - downloading files from server, copy files from one account to another account etc. Threads may take long time to finish there task. My app also control the number of threads to perform operation in queue. With default setting, It can run maximum 5 threads at a time.

我的问题是 - 我的 TableView 没有显示更新的状态和进度。我必须单击列标题来刷新 TableView 。如何使TableView更新aromatically?在后台,如果我运行一个在特定时间间隔后自动触发的线程,用于刷新 TableView 使我的UI冻结。

My Issue is - My TableView does not show the updated status and progress. I have to click on column headers to refresh the TableView. How to make TableView update aromatically? In background, if I run a thread that triggers automatically after specific time interval for refreshing the TableView making my UI freeze.

示例没有显示我在我的应用程序中正在做什么,但我试图证明我面临的问题。运行程序时,您将看到TableView有两列1.名称和2.进度。在启动时,每行的名称将为之前,而您将看到进度条正在运行。我在更新单元格值时添加了1秒的延迟并停止了进度条。

The sample does not show what exactly I am doing in my app but I tried to demonstrate what issue I am facing. When you run the program you will see the TableView with two columns 1. Name and 2. Progress. On start-up Name will will be "Before" for each row whereas the you will see the progress bar running. I have added delay of 1 second in updating cell values and stop progress bar.

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.ProgressBarTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.StackPaneBuilder;
import javafx.stage.Stage;

public class UpdateTableView extends Application {

    private TableView<Person> personTable = new TableView<>();
    private TableColumn colName = new TableColumn<>("Name");
    private TableColumn colProgressBar = new TableColumn("Progress Bar");

    @Override
    public void start(Stage primaryStage) {
        showDialog(primaryStage);

        colName.setCellValueFactory(new PropertyValueFactory("name"));
        colProgressBar.setCellValueFactory(new PropertyValueFactory("progressBar")); // "name" here is for just to render the column
        colProgressBar.setCellFactory(ProgressBarTableCell.forTableColumn());
        personTable.getColumns().addAll(colName, colProgressBar);

        for (int i = 0; i < 10; i++) {
            Person person = new Person();
            person.setProgressBar(-1.0);
            person.setName("Before" + i);
            personTable.getItems().add(person);
        }


        Thread threadAction1 = new Thread() {
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdateTableView.class.getName()).log(Level.SEVERE, null, ex);
                }
                ObservableList<Person> items = personTable.getItems();
                for (int i = 0; i < items.size(); i++) {
                    Person person = items.get(i);
                    person.setName("After" + i);
                    person.setProgressBar(0.0);
                }
            }
        };
        threadAction1.start();
    }

    public void showDialog(Stage primaryStage) {
        try {
            StackPane root = StackPaneBuilder.create().children(personTable).build();
            primaryStage.setScene(new Scene(root, 300, 250));
            primaryStage.show();
        } catch (Exception ex) {
            System.out.println("Exception caught while showing dialog" + ex.getMessage());
        }
    }

    public static class Person {

        private StringProperty name;

        public void setName(String name) {
            this.name = new SimpleStringProperty(name);
        }
        private DoubleProperty progressBar;

        private Person() {
        }

        public double getProgressBar() {
            return progressBar.get();
        }

        public void setProgressBar(double surname) {
            this.progressBar = new SimpleDoubleProperty(surname);
        }

        public Person(String name, double surname) {
            this.name = new SimpleStringProperty(name);
            this.progressBar = new SimpleDoubleProperty(surname);

        }

        public String getName() {
            return name.get();
        }

        public StringProperty nameProperty() {
            return name;
        }

        public DoubleProperty progressBarProperty() {
            return progressBar;
        }
    }

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


推荐答案

你应该更新您的 Person 类,如下所示:

You should have to update your Person class as given below :

public static class Person {

    private StringProperty name;
    private DoubleProperty progressBar;

    private Person() {
       name = new SimpleStringProperty();
       progressBar = new SimpleDoubleProperty();
    }

    public Person(String name, double surname) {
        this.name = new SimpleStringProperty(name);
        this.progressBar = new SimpleDoubleProperty(surname);
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public double getProgressBar() {
        return progressBar.get();
    }

    public void setProgressBar(double surname) {
        this.progressBar.set(surname);
    }

    public String getName() {
        return name.get();
    }

    public StringProperty nameProperty() {
        return name;
    }

    public DoubleProperty progressBarProperty() {
        return progressBar;
    }
}

setter 您正在创建 name &的新对象的方法每次 progressbar ,以便 TableView 未获得更新。

in setter methods you are creating new object of name & progressbar each-time, so that TableView is not getting update.

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

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