进度条的文本区域

Text area for progress bar

我想添加一个文本区域,用户可以在其中看到进度条更新时我可以在控制台中看到的一些信息。

如何添加文本区域?

这是我用来制作进度条的代码示例。我可以在进度条下方添加在计算不足时应填充的文本区域吗?

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class ProgressDialogExample extends Application {
static int option = 0;
static ProgressForm pForm = new ProgressForm();

@Override
public void start(Stage primaryStage) {
    Button startButton = new Button("Start");
    startButton.setOnAction(e -> {

        // In real life this task would do something useful and return
        // some meaningful result:
            Task<Void> task = new Task<Void>() {
                @Override
                public Void call() throws InterruptedException {
                    for (int i = 0; i < 10; i++) {
                        updateProgress(i, 10);
                        Thread.sleep(200);
                    }
                    updateProgress(10, 10);
                    return null;
                }
            };

            // binds progress of progress bars to progress of task:
            pForm.activateProgressBar(task);

            // in real life this method would get the result of the task
            // and update the UI based on its value:
            task.setOnSucceeded(event -> {
                startButton.setDisable(false);
            });

            startButton.setDisable(true);
            pForm.getDialogStage().show();

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

    StackPane root = new StackPane(startButton);
    Scene scene = new Scene(root, 350, 75);
    primaryStage.setScene(scene);
    primaryStage.show();

}

private int closeWindow() {
    return option;
}

private static void setCloseWindow() {
    // TODO Auto-generated method stub
    option = 1;
}

public static class ProgressForm {
    private final Stage dialogStage;
    private final ProgressBar pb = new ProgressBar();
    private final ProgressIndicator pin = new ProgressIndicator();

    public ProgressForm() {
        dialogStage = new Stage();
        dialogStage.initStyle(StageStyle.UTILITY);
        // dialogStage.setResizable(false);
        // dialogStage.setWidth(400);
        // dialogStage.setHeight(300);
        // final VBox vbox = new VBox();
        dialogStage.initModality(Modality.APPLICATION_MODAL);

        final Button exitButton = new Button("Exit");
        exitButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                pForm.getDialogStage().close();
                setCloseWindow();
            }
        });
        // PROGRESS BAR

        pb.setProgress(-1F);
        pin.setProgress(-1F);

        final HBox hb = new HBox();
        hb.setSpacing(5);
        hb.setAlignment(Pos.CENTER);
        hb.getChildren().addAll(pb, pin,  exitButton);

        Scene scene = new Scene(hb);

        dialogStage.setScene(scene);
    }

    public void activateProgressBar(final Task<?> task) {
        pb.progressProperty().bind(task.progressProperty());
        pin.progressProperty().bind(task.progressProperty());
        dialogStage.show();
    }

    public Stage getDialogStage() {
        return dialogStage;
    }
}

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

尝试向任务的 messageProperty 添加一个侦听器,它会在文本发生更改时将其附加到 TextArea。

TextArea ta = new TextArea();

public void activateTextArea(final Task<?> task) {
    task.messageProperty().addListener((property, oldValue, newValue) -> {
         ta.setText(ta.getText() + "\n" + newValue);
    });
}

然后在任务中放入类似这样的东西:

Task<Void> task = new Task<Void>() {
    @Override
    public Void call() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
           updateProgress(i, 10);
           updateMessage((i*10) + "% done");
           Thread.sleep(200);
        }
        updateProgress(10, 10);
        updateMessage("All done!");
        return null;
    }
};

你听说过 Label Labelled 之前使用 Label 而不是你想使用的文本 Node 并且在你想的时候写你的文本想要显示您的进度条

Label.setGraphic(myprogressbarNode);

希望对您有所帮助

编辑

I would like to add a text area where the user can see some information that i can see in the console while the progress bar is updating... Can i add below the progress bar the text area which should fill while computations are mare?

我给了你一个解决方案。

假设您有 ProgressBar pb;并且您正在将 ProgressBar 添加到 StackPane 您不必直接添加 ProgressBar 而是添加您的 Text ,您将通过 Label 实现它所以假设您的 Labellb 这就是您的代码的样子

StackPane sp; // parent
ProgressBar pb; // progress indicator
Lable lb; // my text area

sp.getChildren().add(lb);// i have added the text area
lb.setGraphic(pb); we have added the progressbar to the text area
lb.setContentDisplay(ContentDisplay.***);//the *** represents the position you want your graphic
//whether top or left or bottom
//now you are done, your pb will show aligned to your lb, and be updated
//if you want to show text lb.setText("your text");

这有多含蓄? :-)