在JavaFX表分页中添加进度条

在JavaFX表分页中添加进度条,javafx,javafx-2,javafx-8,Javafx,Javafx 2,Javafx 8,我有这个JavaFX表的例子 import java.util.ArrayList; import java.util.List; import javafx.application.Application; import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.value.ObservableVal

我有这个JavaFX表的例子

    import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class MainApp extends Application
{

    private final static int dataSize = 10_023;
    private final static int rowsPerPage = 1000;

    private final TableView<Sample> table = createTable();
    private final List<Sample> data = createData();

    private List<Sample> createData()
    {
        List<Sample> data = new ArrayList<>(dataSize);

        for (int i = 0; i < dataSize; i++)
        {
            data.add(new Sample(i, "foo " + i, "bar " + i));
        }

        return data;
    }

    private TableView<Sample> createTable()
    {

        TableView<Sample> table = new TableView<>();

        TableColumn<Sample, Integer> column1 = new TableColumn<>("Id");
        column1.setCellValueFactory(param -> param.getValue().id);
        column1.setPrefWidth(150);

        TableColumn<Sample, String> column2 = new TableColumn<>("Foo");
        column2.setCellValueFactory(param -> param.getValue().foo);
        column2.setPrefWidth(250);

        TableColumn<Sample, String> column3 = new TableColumn<>("Bar");
        column3.setCellValueFactory(param -> param.getValue().bar);
        column3.setPrefWidth(250);

        table.getColumns().addAll(column1, column2, column3);

        return table;
    }

    private Node createPage(int pageIndex)
    {

        int fromIndex = pageIndex * rowsPerPage;
        int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
        table.setItems(FXCollections.observableArrayList(data.subList(fromIndex, toIndex)));

        return new BorderPane(table);
    }

    @Override
    public void start(final Stage stage) throws Exception
    {

        Pagination pagination = new Pagination((data.size() / rowsPerPage + 1), 0);
        pagination.setPageFactory(this::createPage);

        Scene scene = new Scene(new BorderPane(pagination), 1024, 768);
        stage.setScene(scene);
        stage.setTitle("Table pager");
        stage.show();
    }

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

    public static class Sample
    {

        private final ObservableValue<Integer> id;
        private final SimpleStringProperty foo;
        private final SimpleStringProperty bar;

        private Sample(int id, String foo, String bar)
        {
            this.id = new SimpleObjectProperty<>(id);
            this.foo = new SimpleStringProperty(foo);
            this.bar = new SimpleStringProperty(bar);
        }
    }
}
import java.util.ArrayList;
导入java.util.List;
导入javafx.application.application;
导入javafx.beans.property.SimpleObject属性;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.value.observeValue;
导入javafx.collections.FXCollections;
导入javafx.scene.Node;
导入javafx.scene.scene;
导入javafx.scene.control.Pagination;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
公共类MainApp扩展应用程序
{
专用最终静态int数据大小=10_023;
私有最终静态int rowsPerPage=1000;
private final TableView table=createTable();
私有最终列表数据=createData();
私有列表createData()
{
列表数据=新的ArrayList(数据大小);
对于(int i=0;iparam.getValue().id);
第1列:设置宽度(150);
TableColumn column2=新的TableColumn(“Foo”);
column2.setCellValueFactory(param->param.getValue().foo);
第2列:设置宽度(250);
TableColumn column3=新的TableColumn(“条形”);
column3.setCellValueFactory(param->param.getValue().bar);
第3列:设置宽度(250);
table.getColumns().addAll(第1列、第2列、第3列);
返回表;
}
专用节点createPage(int pageIndex)
{
int fromIndex=pageIndex*rowsPerPage;
int-toIndex=Math.min(fromIndex+rowsPerPage,data.size());
setItems(FXCollections.observableArrayList(data.subList(fromIndex,toIndex));
返回新的边框窗格(表);
}
@凌驾
public void start(final Stage)引发异常
{
Pagination Pagination=新分页((data.size()/rowsPerPage+1),0);
setPageFactory(this::createPage);
场景=新场景(新边框窗格(分页),1024768);
舞台场景;
stage.setTitle(“表格寻呼机”);
stage.show();
}
公共静态void main(字符串[]args)引发异常
{
发射(args);
}
公共静态类示例
{
私有最终可观察值id;
私人最终SimpleStringProperty foo;
私人物业酒吧;
私有示例(int-id、字符串foo、字符串栏)
{
this.id=新的SimpleObject属性(id);
this.foo=新的SimpleStringProperty(foo);
this.bar=新的SimpleStringProperty(bar);
}
}
}
在切换页面和加载数据时,是否有方法添加进度条

在加载数据时,我不太清楚如何使用进度指示器


是否有任何有用的示例?

最简单的解决方案可能是重写
createPage(int)
方法:

private Node createPage(int pageIndex) {
    Node oldPlaceHolder = table.getPlaceholder();

    ProgressIndicator progress = new ProgressIndicator();
    progress.setMaxSize(90, 90);
    table.setPlaceholder(new StackPane(progress));
    table.getItems().clear();

    Task<ObservableList<Sample>> task = new Task<ObservableList<Sample>>() {

        @Override
        protected ObservableList<Sample> call() throws Exception {
            for(int i = 0; i < 99; i++)
            {
                Thread.sleep(20);
                updateProgress(i, 100);
            }
            int fromIndex = pageIndex * rowsPerPage;
            int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
            return (FXCollections.observableArrayList(data.subList(
                    fromIndex, toIndex)));
        }
    };

    progress.progressProperty().bind(task.progressProperty());
    task.setOnSucceeded(ev -> {
        table.setItems(task.getValue());
        table.setPlaceholder(oldPlaceHolder);
    });
    new Thread(task).start();

    return new BorderPane(table);
}
  • 表格视图
  • 创建一个用于计算下一页内容的脚本(在我的示例中,只是一个执行
    Thread.sleep()
    )的虚拟脚本)
  • 完成
    任务后
    将占位符替换为旧占位符(备份和还原),并将数据设置为
    表视图
  • 这是
    createPage()
    方法的一个小示例:

    private Node createPage(int pageIndex) {
        Node oldPlaceHolder = table.getPlaceholder();
    
        ProgressIndicator progress = new ProgressIndicator();
        progress.setMaxSize(90, 90);
        table.setPlaceholder(new StackPane(progress));
        table.getItems().clear();
    
        Task<ObservableList<Sample>> task = new Task<ObservableList<Sample>>() {
    
            @Override
            protected ObservableList<Sample> call() throws Exception {
                for(int i = 0; i < 99; i++)
                {
                    Thread.sleep(20);
                    updateProgress(i, 100);
                }
                int fromIndex = pageIndex * rowsPerPage;
                int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
                return (FXCollections.observableArrayList(data.subList(
                        fromIndex, toIndex)));
            }
        };
    
        progress.progressProperty().bind(task.progressProperty());
        task.setOnSucceeded(ev -> {
            table.setItems(task.getValue());
            table.setPlaceholder(oldPlaceHolder);
        });
        new Thread(task).start();
    
        return new BorderPane(table);
    }
    
    private节点createPage(int-pageIndex){
    节点oldPlaceHolder=table.getPlaceholder();
    ProgressIndicator进度=新的ProgressIndicator();
    进度.setMaxSize(90,90);
    表.setPlaceholder(新堆栈窗格(进度));
    table.getItems().clear();
    任务=新任务(){
    @凌驾
    受保护的ObservableList调用()引发异常{
    对于(int i=0;i<99;i++)
    {
    睡眠(20);
    updateProgress(i,100);
    }
    int fromIndex=pageIndex*rowsPerPage;
    int-toIndex=Math.min(fromIndex+rowsPerPage,data.size());
    return(FXCollections.observearraylist)(data.subList(
    从索引到索引);
    }
    };
    progressProperty().bind(task.progressProperty());
    任务设置成功(ev->{
    table.setItems(task.getValue());
    table.setPlaceholder(旧占位符);
    });
    新线程(任务).start();
    返回新的边框窗格(表);
    }
    
    最简单的解决方案可能是重写
    createPage(int)
    方法:

    private Node createPage(int pageIndex) {
        Node oldPlaceHolder = table.getPlaceholder();
    
        ProgressIndicator progress = new ProgressIndicator();
        progress.setMaxSize(90, 90);
        table.setPlaceholder(new StackPane(progress));
        table.getItems().clear();
    
        Task<ObservableList<Sample>> task = new Task<ObservableList<Sample>>() {
    
            @Override
            protected ObservableList<Sample> call() throws Exception {
                for(int i = 0; i < 99; i++)
                {
                    Thread.sleep(20);
                    updateProgress(i, 100);
                }
                int fromIndex = pageIndex * rowsPerPage;
                int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
                return (FXCollections.observableArrayList(data.subList(
                        fromIndex, toIndex)));
            }
        };
    
        progress.progressProperty().bind(task.progressProperty());
        task.setOnSucceeded(ev -> {
            table.setItems(task.getValue());
            table.setPlaceholder(oldPlaceHolder);
        });
        new Thread(task).start();
    
        return new BorderPane(table);
    }
    
  • 表格视图
  • 创建一个用于计算下一页内容的脚本(在我的示例中,只是一个执行
    Thread.sleep()
    )的虚拟脚本)
  • 完成
    任务后
    将占位符替换为旧占位符(备份和还原),并将数据设置为
    表视图
  • 这是
    createPage()
    方法的一个小示例:

    private Node createPage(int pageIndex) {
        Node oldPlaceHolder = table.getPlaceholder();
    
        ProgressIndicator progress = new ProgressIndicator();
        progress.setMaxSize(90, 90);
        table.setPlaceholder(new StackPane(progress));
        table.getItems().clear();
    
        Task<ObservableList<Sample>> task = new Task<ObservableList<Sample>>() {
    
            @Override
            protected ObservableList<Sample> call() throws Exception {
                for(int i = 0; i < 99; i++)
                {
                    Thread.sleep(20);
                    updateProgress(i, 100);
                }
                int fromIndex = pageIndex * rowsPerPage;
                int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
                return (FXCollections.observableArrayList(data.subList(
                        fromIndex, toIndex)));
            }
        };
    
        progress.progressProperty().bind(task.progressProperty());
        task.setOnSucceeded(ev -> {
            table.setItems(task.getValue());
            table.setPlaceholder(oldPlaceHolder);
        });
        new Thread(task).start();
    
        return new BorderPane(table);
    }
    
    private节点createPage(int-pageIndex){
    节点oldPlaceHolder=table.getPlaceholder();
    ProgressIndicator进度=新的ProgressIndicator();
    进度.setMaxSize(90,90);
    表.setPlaceholder(新堆栈窗格(进度));
    table.getItems().clear();
    任务=新任务(){
    @凌驾
    受保护的ObservableList调用()引发异常{
    对于(int i=0;i<99;i++)
    {
    睡眠(20);
    updateProgress(i,100);
    }
    int fromIndex=pageIndex*rowsPerPage;
    int-toIndex=Math.min(fromIndex+rowsPerPage,data.size());
    return(FXCollections.observearraylist)(data.subList(
    从索引到索引);
    }
    };
    进步