PreLoader 的多线程 - JavaFX

2024-04-25

我正在开发一个 JavaFX 应用程序,需要在启动主应用程序阶段之前从文件中加载资源。我完成此任务的解决方案是使用 PreLoader,以便用户在加载资源之前无法与应用程序交互(非常标准的东西)。

我有一个扩展 PreLoader 类的类,该类创建一个加载 .fxml 文件的对象,该文件是 PreLoader 场景将显示的内容。它工作得很好,直到我尝试插入代码来加载文件。

我想同时执行此操作,以便在加载文件期间状态会更新到屏幕上。我已经研究过使用此处的任务和论坛,但没有任何帮助,因为我不知道将代码放在哪里。

扩展 PreLoader 的类:

public class SplashPreLoader extends Preloader {
    private Stage stage;
    private SplashScene loadScreen;

    public void start(Stage stage) throws Exception {
        SplashScene intro = new SplashScene();
        this.loadScreen = intro;
        this.stage = stage;
        stage.setScene(new Scene(intro, 475, 425));
        stage.initStyle(StageStyle.UNDECORATED);
        stage.show();
    }

    @Override
    public void handleProgressNotification(ProgressNotification pn) {
        //bar.setProgress(pn.getProgress());
    }

    @Override
    public void handleStateChangeNotification(StateChangeNotification evt) {
        if (evt.getType() == StateChangeNotification.Type.BEFORE_LOAD) {
            //loadScreen.setStatusMessage("Hello Mister");
        }
        if (evt.getType() == StateChangeNotification.Type.BEFORE_START) {
      /*      Task<Void> task = new Task<Void>() {
                @Override
                public Void call() {
                    updateMessage("this mesashlkjfhkjlsd");
                    return null ;
                }
            };
            statusMessage.textProperty().bind(task.messageProperty());
            task.messageProperty().addListener((obs, oldMessage, newMessage) -> loadScreen.setStatusMessage(newMessage));
            Thread th = new Thread(task);
            th.setDaemon(true);
            th.start();*/

            //loadScreen.loadWebDriver();

            stage.hide();
        }
}

场景组件:

public class SplashScene extends StackPane {

    @FXML public Label statusMessage;

    public SplashScene() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("splash_scene.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        //statusMessage.setTextFill(Color.WHITE);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

    }

    public String getStatusMessage() { return statusMessageProperty().getValue(); }

    public void setStatusMessage(String value) {
        statusMessageProperty().set(value);
        System.out.println("at hert");
    }

    public StringProperty statusMessageProperty() { return statusMessage.textProperty(); }

    public void loadWebDriver() { 
        //This is the function that I want to call to load all the files. 
    }

statusMessage是我要在加载文件时修改的标签。我尝试将任务(线程)放入loadWebDriver()函数的末尾处start()在前面的 if 子句中,但它没有产生我想要的结果。

我也觉得很奇怪,因为当我在没有task,我在文件加载代码之前有标签更改代码,但它们总是以相反的顺序执行。

我觉得这可以从文档中得到帮助,但这对我来说没有任何意义..有人知道这意味着什么吗?

请注意,预加载器与其他 JavaFX 应用程序(包括 FX 线程)遵循相同的规则 规则。特别是,类构造函数和 init() 方法将在非 FX 线程上调用,并且 start() 将在 FX 应用程序线程上执行。这也意味着应用程序构造函数/init() 将与预加载器 start() 同时运行。

我需要使用 init() 方法吗?


这是一个示例,希望对您有所帮助。你可以做的一件事是使用Task加载所有数据。当。。。的时候Task完成了,使用来自的想法将 MVC 与 JavaFX 一起应用 https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx通过Model to any Controller需要它。

Main

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;
import javafx.concurrent.Task;

/**
 * JavaFX App
 */
public class App extends Application {



    @Override
    public void start(Stage primaryStage) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("primary.fxml"));
        Parent root = loader.load();
        PrimaryController primaryController = loader.getController();

        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();

        final Task<DataModel> task = new Task<DataModel>(){
            @Override
            protected DataModel call() throws Exception
            {
                updateProgress(0, 3);
                DataModel dataModel = new DataModel();
                dataModel.loadListViewData();
                Thread.sleep(2000);
                updateProgress(1, 3);
                dataModel.loadComoBoxData();
                Thread.sleep(2000);
                updateProgress(2, 3);
                dataModel.loadTextAreaData();
                Thread.sleep(2000);
                updateProgress(3, 3);
                Thread.sleep(1000);

                return dataModel;
            }
        };

        task.setOnSucceeded((event) -> {             
            try
            {   
                FXMLLoader secondaryLoader = new FXMLLoader(getClass().getResource("secondary.fxml"));
                Stage secondaryStage = new Stage();
                Parent secondaryRoot = secondaryLoader.load();
                SecondaryController secondaryController = secondaryLoader.getController();
                secondaryController.initModel(task.getValue());
                secondaryStage.setTitle("Scene One");
                secondaryStage.setScene(new Scene(secondaryRoot, 500, 500));
                secondaryStage.show();
                primaryStage.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }

        });

        primaryController.getPBSplashValue().progressProperty().bind(task.progressProperty());
        primaryController.getPISplash().progressProperty().bind(task.progressProperty());

        new Thread(task).start();
    }

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

}

主控制器/启动画面

import javafx.fxml.FXML;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;

public class PrimaryController
{
    @FXML
    ProgressBar pbSplash;
    @FXML
    ProgressIndicator piSplash;


    public ProgressBar getPBSplashValue()
    {
        return pbSplash;
    }

    public ProgressIndicator getPISplash()
    {
        return piSplash;
    }

}

主要FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sed.test.maventtestproject.PrimaryController">
   <children>
      <VBox maxHeight="-Infinity" maxWidth="-Infinity">
         <children>
            <ProgressBar fx:id="pbSplash" prefWidth="200.0" progress="0.0" />
            <ProgressIndicator fx:id="piSplash" progress="0.0" />
         </children>
      </VBox>
   </children>
</StackPane>

SecondayController/FirstSceneAfterSplashScreen

import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;

public class SecondaryController {
    @FXML ListView<String> lvMain;
    @FXML ComboBox<String> cbMain;
    @FXML TextArea taMain;

    private DataModel model ;

    public void initModel(DataModel model) {
        if (this.model != null) {
            throw new IllegalStateException("Model can only be initialized once");
        }
        this.model = model ;

        lvMain.setItems(this.model.getListViewData());
        cbMain.setItems(this.model.getComboBoxData());
        taMain.setText(this.model.getTextAreaData());
    }
}

辅助FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.VBox?>

<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sed.test.maventtestproject.SecondaryController">
    <children>
      <ListView fx:id="lvMain" prefHeight="200.0" prefWidth="200.0" />
      <ComboBox fx:id="cbMain" prefWidth="150.0" />
      <TextArea fx:id="taMain" prefHeight="200.0" prefWidth="200.0" wrapText="true" />
    </children>
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
    </padding>
</VBox>

Model

import java.util.ArrayList;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 *
 * @author blj0011
 */
public class DataModel
{
    private final ObservableList<String> listViewData = FXCollections.observableArrayList();
    private final ObservableList<String> comboBoxwData = FXCollections.observableArrayList();
    private final StringProperty textAreaDataProperty = new SimpleStringProperty();

    public DataModel()
    {
    }

    public void loadListViewData()
    {
        listViewData.addAll(retrieveListViewDataFromDB());
    }

    public ObservableList<String> getListViewData()
    {
        return listViewData;
    }

    public void loadComoBoxData()
    {
        comboBoxwData.addAll(retrieveComboBoxDataFromDB());        
    }

    public ObservableList<String> getComboBoxData()
    {
        return comboBoxwData;
    }    

    public void loadTextAreaData()
    {
        textAreaDataProperty.set(retrieveTextAreaDataFromDB());
    }

    public StringProperty getTextAreaDataProperty()
    {
        return textAreaDataProperty;
    }

    public String getTextAreaData()
    {
        return textAreaDataProperty.get();
    }


    //Private methods that fake retrieving data from the database.
    private List<String> retrieveListViewDataFromDB()
    {
        List<String> dataFromDB = new ArrayList();
        for(int i = 0; i < 1000; i++)
        {
            dataFromDB.add(Integer.toString(i));
        }

        return dataFromDB;
    }    

    private List<String> retrieveComboBoxDataFromDB()
    {
        List<String> dataFromDB = new ArrayList();
        dataFromDB.add("A");
        dataFromDB.add("B");
        dataFromDB.add("C");
        dataFromDB.add("D");
        dataFromDB.add("E");
        dataFromDB.add("F");

        return dataFromDB;
    }

    private String retrieveTextAreaDataFromDB()
    {
        return "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";  
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PreLoader 的多线程 - JavaFX 的相关文章

随机推荐

  • 在 SPSS 18 中指定相对路径

    在 SPSS 11 中可以指定相对路径 例子 FILE HANDLE myfile data current txt LRECL 533 DATA LIST FILE myfile 这之所以有效 是因为 SPSS 11 将工作文件夹设置为源
  • 操作员 '??'不能应用于类型“T”和“T”的操作数

    我有以下通用方法 但 VS 给了我一个编译错误 运算符 不能应用于 T 和 T 类型的操作数 public static T Method
  • Google 地图 - 未捕获 InvalidValueError:初始化不是函数

    当我加载 Google 地图显示的页面时 我总是在控制台中看到以下错误 未捕获的 InvalidValueError 初始化不是函数js 传感器 假 回调 初始化 94 将鼠标悬停在文件名上时 这显示为源自 谷歌地图窗口和地图显示得非常好
  • 在RequiredFieldValidator 触发后调用JavaScript 方法?

    表单元素被视为无效后是否可以触发 JavaScript 方法 这是我的场景 ASPX 页面上有 2 个选项卡 用户必须在两个选项卡上填写信息 用户在选项卡 2 上单击提交按钮 但是 第一个选项卡上有一个必填字段需要注意 我是否需要创建自定义
  • 无法使用 conda 安装 mpi4py 并指定预安装的 mpicc 路径

    我已经尝试安装mpi4py with env MPICC path to openmpi bin mpicc conda install c anaconda mpi4py 但我收到这样的消息 The following NEW packa
  • 无法播种用户和角色

    我正在尝试将用户和角色植入我的数据库中 目前在 C MVC4 中使用具有自动迁移功能的 Code First 实体框架 每当我打电话 更新数据库 强制 我收到以下错误 运行种子方法 System InvalidOperationExcept
  • B 样条线混淆

    我意识到这个板上有关于 B 样条主题的帖子 但这些帖子实际上让我更加困惑 所以我想有人可能能够帮助我 我有 x 值范围从 0 到 1 的模拟数据 我想用三次样条拟合我的数据 degree 3 结为 0 0 1 0 2 0 9 1 我还想使用
  • 从日期选择器小部件中隐藏年份

    我试图从日期选择器小部件中隐藏年份字段 这可能看起来像一个重复的问题 但之前问题给出的答案并不能帮助我隐藏年份字段 这是我的代码 Override protected void onCreate Bundle savedInstanceSt
  • 为什么没有像 ruby​​ 的 capybara for java 这样的无头 BDD 测试引擎?

    为什么没有 java 版本的 ruby capybara https github com jnicklas capybara https github com jnicklas capybara 这可以与 tomcat 或其他东西一起使用
  • 如何在 PyGame 中加载多个图像?

    我需要在 pygame 中加载大约 200 个图像 以便在游戏中的各个点进行位块传输 我尝试为此编写一个函数 但总是回来NameError name tomato is not defined 所有图像名称都是加载图像的变量存储的名称 to
  • 设置分组条形图之间的间距

    我正在尝试按照图库中的示例在 matplotlib 中制作分组条形图 我使用以下内容 import matplotlib pyplot as plt plt figure figsize 7 7 dpi 300 xticks 0 1 1 1
  • 本地播放 mp3 时 Android MediaPlayer 错误(1, -2147483648)

    Android 4 4 发布后不久 我的代码自 2 2 以来一直正常工作 突然出现了错误 我的代码将查询音乐数据库中给定的音乐文件 该路径在查询中返回 然后该路径被传递到 MediaPlayer Code String uri conten
  • 在reactjs中以编程方式取消选中复选框

    我正在弄乱复选框 我想知道有没有一种方法可以通过调用函数来取消选中单击按钮时的复选框 如果是这样 我怎样才能做到这一点
  • 实现类 Markdown 语言的解析器

    我有类似于 markdown 和 SO 使用的标记语言 遗留解析器基于正则表达式 维护起来简直是噩梦 因此我提出了自己的基于 EBNF 语法的解决方案 并通过 mxTextTools SimpleParse 实现 但是 某些令牌可能存在相互
  • CMake 64 位和 SFML 64 位

    我正在尝试使用适用于 Windows 的 CMake 64 位和 SFML 2 5 1 64 位构建 C 项目 当我在项目上运行 cmake 时 我收到一条错误消息 我能让它工作的唯一方法是改变CMAKE PREFIX PATH指向 SFM
  • 读取请求正文两次

    我试图读取中间件中的主体以进行身份 验证 但是当请求到达 api 控制器时 对象为空 因为主体已被读取 有没有办法解决 我在中间件中像这样读取正文 var buffer new byte Convert ToInt32 context Re
  • 如果图像比其容器宽,如何将图像居中?

    通常 您将图像居中display block margin auto 但如果图像比容器大 则会向右溢出 如何使其均匀地向两侧溢出 容器的宽度是固定且已知的 图像的宽度未知 纯CSS解决方案 需要一个额外的包装器 在 FireFox IE8
  • 不同数据类型的内存对齐方式是否不同

    在C中执行不同的数据类型 例如char short int long float double有不同的内存对齐边界吗 在 32 位字对齐字节可寻址操作系统中 如何访问char or short与访问不同int or float 在这两种情况
  • html5在移动设备中自动播放视频

    Auto play is not working without muted attribute when I try to open url in mobile device How to play video without using
  • PreLoader 的多线程 - JavaFX

    我正在开发一个 JavaFX 应用程序 需要在启动主应用程序阶段之前从文件中加载资源 我完成此任务的解决方案是使用 PreLoader 以便用户在加载资源之前无法与应用程序交互 非常标准的东西 我有一个扩展 PreLoader 类的类 该类