【问题标题】:Text area for progress bar进度条的文本区域
【发布时间】:2015-07-27 13:27:35
【问题描述】:

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

如何添加文本区域?

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

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);
}
}

【问题讨论】:

    标签: user-interface text javafx progress-bar


    【解决方案1】:

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

    Label.setGraphic(myprogressbarNode);
    

    希望对你有帮助

    编辑

    我想添加一个文本区域,用户可以在其中看到一些我可以在进度条更新时在控制台中看到的信息...我可以在进度条下方添加计算时应该填充的文本区域吗母马?

    我给了你一个解决方案。

    假设你有你的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");
    

    这是多么隐含? :-)

    【讨论】:

    • 你能再明确一点吗?我不明白解决方案
    【解决方案2】:

    尝试向任务的 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;
        }
    };
    

    【讨论】:

    • 它不起作用。它给了我一个属性和新值的错误
    • 刚刚注意到我发布的代码中有一个错字。尝试在 oldValue 和 newValue 之间加一个逗号
    • 仍然,当我将“ta”添加到 GridPane 时,我得到一个错误,因为我必须将 ta 声明为静态。有什么解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2017-09-06
    • 1970-01-01
    相关资源
    最近更新 更多