Java FX 8在应用程序init()方法中使用Platform.runLater()来显示警报/登录框

时间:2019-03-18 14:16:39

标签: java javafx javafx-8

请多多包涵,因为这将是非常详细和具体的要求。我还研究了许多已经在stackoverflow上列出的有关Platform.runLater()使用情况的问题。但是我找不到合适的答案。如果有人能识别出正确的问题并将其标记为重复,我将非常满意。另外,我还是JavaFX编程的新手,所以如果需要修改体系结构,请告诉我。

我正在创建一个独立的JavaFX应用程序,以根据用户定义的要求运行某些单元测试。在运行实际测试之前,我需要通过比较远程Artifactory存储库中的清单文件和当前类路径中jar中的清单文件,确保最终用户正在使用具有最新可用库的测试。我想存储Artifactory的凭据,以便可以将其重新用于后续执行。如果远程和本地清单的内部版本号不匹配,那么我想显示一个错误警报窗口,通知用户。我想在每个步骤中使用预加载器显示gif和适当的消息。

public class UI extends Application 
{
    String username=null, password=null;
    Manifest remoteManifest, localManifest;

    @Override
    public void init() {

        notifyPreloader("Loading Stored Credentials");
        loadStoredCredentials();

        if(username == null && password == null) {
            notifyPreloader("Requesting New Credentials");
            getNewCredentials();
        }

        notifyPreloader("Reading remote manifest");
        readRemoteManifest();

        notifyPreloader("Reading local manifest");
        readLocalManifest();

        notifyPreloader("Comparing manifests");
        compareManifests();

    }

    private void getNewCredentials() {
        Platform.runLater(new Runnable() {
            public void run() {
                // Login window for new credentials
                //Save credentials
            }
        });
    }

    private void readRemoteManifest() {
        // REST API call to read remote manifest
        if(HttpStatus.SC_UNAUTHORIZED) { // Stored credentials might have been read the first time which are expired, so try again.
            getNewCredentials();
            readRemoteManifest();
        } else {
            // update remote manifest object
        }
    }

    private void readLocalManifest() {
        // Update local manifest object
    }

    private void showAlert() {
        Platform.runLater( new Runnable() {
            // Display Alert window
        });
    }

    private void compareManifests() {
        if(remoteManifest == null) {
            showAlert();
        }

        if(localAlert == null) {
            showAlert();
        }

        if(remoteBuildNumber != localBuildNumber) {
            showAlert();
        }
    }

    @Override
    public void start(Stage primaryStage){
        //UI to select options and run tests
    }
}

这种方法存在三个问题。

  1. 如果我不使用Alert Windows和Login Windows的Platform.runLater(),那么我会收到一条错误消息,提示不在FX应用程序线程上; currentThread = JavaFX应用程序线程错误?
  2. 如果我使用Platform.runLater(),则即使登录窗口处于活动状态,其余的警报窗口也会立即显示,而无需等待读取登录凭据。
  3. 如果我手动创建文件以存储当前凭据,那么我拥有的任何警报窗口都会显示两次。

让我知道是否需要其他信息。 预先感谢。

EDIT-1:

有人给我一个解决前两个问题的答案。但是现在答案不见了。如果他们可以重新发布答案,则对其他人会有所帮助。

解决方案是将Platform.runLater()代码从showAlert()方法移到compareManifests()方法。相反,我将Platform.runLater()移至init()方法。现在我只有一个调用runLater()。

修改后的代码可解决前两个问题。

public class UI extends Application 
{
    String username=null, password=null;
    Manifest remoteManifest, localManifest;

    @Override
    public void init() {
        Platform.runLater( new Runnable() {
            public void run() {
                notifyPreloader("Loading Stored Credentials");
                loadStoredCredentials();

                if(username == null && password == null) {
                    notifyPreloader("Requesting New Credentials");
                    getNewCredentials();
                }

                notifyPreloader("Reading remote manifest");
                readRemoteManifest();

                notifyPreloader("Reading local manifest");
                readLocalManifest();

                notifyPreloader("Comparing manifests");
                compareManifests();
            }
        });
    }

    private void getNewCredentials() {
        // Login window for new credentials
        //Save credentials
    }

    private void readRemoteManifest() {
        // REST API call to read remote manifest
        if(HttpStatus.SC_UNAUTHORIZED) { // Stored credentials might have been read the first time which are expired, so try again.
            getNewCredentials();
            readRemoteManifest();
        } else {
            // update remote manifest object
        }
    }

    private void readLocalManifest() {
        // Update local manifest object
    }

    private void showAlert() {
        // Display Alert window
    }

    private void compareManifests() {

        if(remoteManifest == null) {
            showAlert();
        }

        if(localAlert == null) {
            showAlert();
        }

        if(remoteBuildNumber != localBuildNumber) {
            showAlert();
        }
    }

    private void loadStoredCredentials() {
        // Read the credentials from a local file
    }

    @Override
    public void start(Stage primaryStage){
        //UI to select options and run tests
    }
}

问题3仍在发生,现在显示两个登录窗口。

谢谢。

0 个答案:

没有答案