In this JavaFX source code example, we will see how to use the JavaFX ProgressBar control with an example.
ProgressBar is a control that indicates the processing of a particular task with a completion bar.
Java JavaFX
JavaFX ProgressBar Example
The example consists of a progress bar and a button. The button starts the progress bar that is animated for a few seconds.package sample; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ProgressBar; import javafx.scene.layout.HBox; import javafx.stage.Stage; import javafx.util.Duration; public class Main extends Application { @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { var root = new HBox(15); root.setAlignment(Pos.CENTER); root.setPadding(new Insets(10)); var pbar = new ProgressBar(0); pbar.setPrefWidth(150); var frame1 = new KeyFrame(Duration.ZERO, new KeyValue(pbar.progressProperty(), 0)); var frame2 = new KeyFrame(Duration.seconds(15), new KeyValue(pbar.progressProperty(), 1)); var task = new Timeline(frame1, frame2); var btn = new Button("Start"); btn.setOnAction((ActionEvent actionEvent) -> task.playFromStart()); root.getChildren().addAll(pbar, btn); var scene = new Scene(root); stage.setTitle("ProgressBar"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
Output:
Comments
Post a Comment