Korlimann
Korlimann

Reputation: 147

JavaFX - Any way to enter the JavaFX-Thread while executing a background task?

I have the following Task, where I import a file. The method starts a Dialog with a ProgressBar, and the progressProperty from the ProgressBar is bound to the progressProperty of the task. Now I want to check if the ProgressBar is already done, but I have to ProgressBar in a special class and I can't access methods from any other classes while executing the Task. Now, my question is, how can I ensure that the program checks if the ProgressBar is done, because my Dialog will only close if the ProgressBar is finished, and at the current moment, the Dialog never closes. Here is my code:

public void readFile(File chat) {
    Task<Void> task = new Task<Void>() {

        @Override
        protected Void call() throws Exception {
            if(chat.getName().contains("KakaoTalk_")) {
                String s = "";
                String gesamt = "";
                double laenge = 0;
                try(BufferedReader brCount = new BufferedReader(new FileReader(chat))) {
                    while((s=brCount.readLine())!=null) {
                        laenge++;
                    }
                } catch (IOException e) {
                    System.out.println("Fehler beim zählen");
                }
                double momentanErreicht = 0;
                try(BufferedReader br = new BufferedReader(new FileReader(chat))) {
                    while((s=br.readLine())!=null) {
                        momentanErreicht++;
                        updateProgress(momentanErreicht, laenge);
                        s = s.replace("ß", "ß");
                        s = s.replace("ö", "ö");
                        s = s.replace("ü", "ü");
                        s = s.replace("ä", "ä");
                        s = s.replace("Ä", "Ä");
                        s = s.replace("Ãœ", "Ü");
                        s = s.replace("Ö", "Ö");
                        gesamt += s+"\n";
                    }
                } catch (FileNotFoundException e1) {
                    System.out.println("File not found");
                } catch (IOException e2) {
                    System.out.println("IOException");
                }
                mp.isFortschrittDialogCompleted();
                mp.eingabeSetText(gesamt);
                setChat(mp.eingabeGetText());
                getChat();
            } else mp.mhNichtPassendesFile();
            return null;
        }
    };
    mp.progressP().bind(task.progressProperty());
    mp.startFortschrittDialog();
    Thread th = new Thread(task);
    th.setDaemon(true);
    th.start();
    mp.isFortschrittDialogCompleted();
}

Here is also my MyRootPane (mp) where the executed methods lead to:

    public void eingabeSetText(String eingabe) {
        this.eingabe.setText(eingabe);
    }
    public String eingabeGetText() {
        return eingabe.getText();
    }
    public void startFortschrittDialog() {
        fd.show();
    }
    public void endFortschrittDialog() {
        fd.close();
    }
    public void isFortschrittDialogCompleted() {
        if(fd.isCompleted()) endFortschrittDialog();
    }
    public DoubleProperty progressP() {
        return fd.getPBProgressProperty();
    }

And the Dialog with the ProgressBar:

public class FortschrittDialog extends Dialog {

    private ProgressBar pb = new ProgressBar();

    public FortschrittDialog() {
        pb.setPrefWidth(500);
        pb.setProgress(-1f);

        getDialogPane().setContent(pb);
    }
    public DoubleProperty getPBProgressProperty() {
        return pb.progressProperty();
    }
    public boolean isCompleted() {
        if(pb.getProgress()==1.0) return true;
        else return false;
    }
}

I hope anyone understands my problem and can give me a quick and easy solutions, if possible even with explanation. If your missing something of the code, please tell

Upvotes: 0

Views: 97

Answers (1)

Sunflame
Sunflame

Reputation: 3186

according to one of this javafx.scene.control.Dialog<R> won't close on pressing "x" question's answer, you can only close the dialog if you have a defined button on your dialog pane, then you can do somethig like this:

    Window window = dialog.getDialogPane().getScene().getWindow();
    window.setOnCloseRequest(event -> dialog.close());

Then as @James_D mentioned in a comment you can use

    task.setOnSucceeded(event -> window.hide());

this is the relevant part for you from the answer:

JavaFX dialogs can only be closed 'abnormally' (as defined above) in two situations:

  • When the dialog only has one button,
  • When the dialog has multiple buttons, as long as one of them meets one of the following requirements:

    • The button has a ButtonType whose ButtonData is of type ButtonData.CANCEL_CLOSE.
    • The button has a ButtonType whose ButtonData returns true when ButtonData.isCancelButton() is called. ...

If you are using this solution you don't have to use the doubleProperty and the isCompleted() methods.

Upvotes: 1

Related Questions