2

我试图在JavaFX TableView. 每个任务由一行表示成一个TableView

在此处输入图像描述

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

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

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

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

1 回答 1

4

您应该更新您的Person课程,如下所示:

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&的新对象,因此不会更新。progressbarTableView

于 2012-12-14T07:18:01.423 回答