JavaFX:不在FX应用程序线程错误发生

时间:2015-12-08 09:01:53

标签: java javafx

我希望在某些任务正常工作时弹出progressindecator。为了做到这一点,我将我的程序编码如下......

length(names)

有一个函数可以更改checkTargetDatabaseInfo方法中的标签。

和这样的错误..

public void handleCheckButtonOnAction(Button button, DBConnect conn) {
    ProgressForm pForm = new ProgressForm();
    Task<Void> task = new Task<Void>() {
        @Override
        public Void call() throws InterruptedException {
            checkTargetDatabaseInfo();
            updateProgress(-1, -1);
            return null;
        }
    };
    pForm.activateProgressBar(task);
    task.setOnSucceeded(event -> {
        pForm.getDialogStage().close();
    });

    pForm.getDialogStage().show();

    Thread thread = new Thread(task);
    thread.start();
}

1 个答案:

答案 0 :(得分:1)

安德烈亚斯,我终于找到了自己的答案,无论如何它非常感谢,你的评论对我很有帮助。

我插入了一些将UI方法更新到Platform.runLater方法的代码,它工作正常。这是我的代码..

private void checkDBVersion () {
        ProgressForm pForm = new ProgressForm();
        Task<Void> task = new Task<Void>() {
            @Override
            public Void call() throws InterruptedException {
                SimpleStringProperty version = new SimpleStringProperty();
                Connection targetDBconn = null;
                PreparedStatement pstmt = null;
                ResultSet rs = null;

                try {
                    targetDBconn = globalTargetConn.connect();
                    Platform.runLater(new Runnable(){@Override public void run() {logWriter.writeLogs(logTextArea, LogWriter.INFO, "Checking this database version...");}});
                    pstmt = new LoggableStatement(targetDBconn, SqlList.checkTargetDBVersionSQL);
                    String SQL = ((LoggableStatement) pstmt).getQueryString();
                    Platform.runLater(new Runnable(){@Override public void run() {logWriter.writeLogs(logTextArea, LogWriter.INFO, "See the query below...");}});
                    Platform.runLater(new Runnable(){@Override public void run() {logWriter.writeLogs(logTextArea, LogWriter.INFO, SQL);}});
                    rs = pstmt.executeQuery();
                    while (rs.next()) {
                        version.set(rs.getString(1));
                        targetDatabaseInfo.setVersion(version);
                    }
                    Platform.runLater(new Runnable(){@Override public void run() {logWriter.writeLogs(logTextArea, LogWriter.INFO, "Your database version is " + version.getValue());}});
                } catch (SQLException ex) {Platform.runLater(new Runnable(){@Override public void run() {logWriter.writeLogs(logTextArea, LogWriter.ERROR, ex.getMessage());}}); 
                } finally {
                    if (rs != null ) try {rs.close();} catch(SQLException ex) {}
                    if (pstmt != null ) try {pstmt.close();} catch(SQLException ex) {}
                    if (targetDBconn != null ) try {targetDBconn.close();} catch(SQLException ex) {}
                }
                _DB_VERSION = version;
                updateProgress(-1, -1);
                return null;
            }
        };
        pForm.activateProgressBar(task);
        task.setOnSucceeded(event -> {
            pForm.getDialogStage().close();
        });
        pForm.getDialogStage().show();
        Thread thread = new Thread(task);
        thread.start();
    }

完成此操作后,我只是好奇是否是后台工作而不是。是进度条后台工作??