Javafx progressbar on finish an action happens(eg enabling a disabled button).



import javafx.application.Application;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.Button;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class ProgressBarActivateBtn extends Application
{
    public static void main(String[]args)
    {
        Application.launch(args);
    }
    @Override
   public void start(Stage primaryStage)
   {
       BorderPane bp = new BorderPane();
       Scene scene = new Scene(bp,600,400);
       
       //creating progressbar
       ProgressBar pbar = new ProgressBar(0);
       pbar.setPrefWidth(400);//setting the width of the progressbar
       pbar.setPrefHeight(20);//setting the height of the progressbar
       
       
       KeyFrame frame1 = new KeyFrame(Duration.ZERO,new KeyValue(pbar.progressProperty(),0));//keyframe defines target values at a specified point in time for a set of variables are interpolated along a timeline
       KeyFrame frame2 = new KeyFrame(Duration.seconds(10),new KeyValue(pbar.progressProperty(),1));//The progress bar will take 10 seconds to load
       
       Timeline task = new Timeline(frame1,frame2);//a timeline can be used to define a free form value of any writeable value
       
     //creating the button
       Button btn = new Button("next");
       btn.setDisable(true);//disabling the button
       
       FlowPane fp = new FlowPane();
       fp.setAlignment(Pos.CENTER);
       fp.setHgap(10);//setting the horizontal gap for controls in the flowpane
       fp.getChildren().addAll(pbar,btn);//putting the progressbar and button on the flowpane
       
       bp.setCenter(fp);//setting the flowpane at the center of the borderpane
       task.play();//it starts the progressbar
       
       //after the progressbar has finished loading the button is enabled
       task.setOnFinished((ActionEvent t) -> {
            btn.setDisable(false);
        });
       
       primaryStage.setTitle("ProgressBar On Finish Action");
       primaryStage.setScene(scene);
       primaryStage.show();
       
   }

}

The code above produces the result above:






Comments

Popular posts from this blog

Javafx adding icons to menu and menuitem.

Javafx inserting uploaded image into database.

Javafx displaying image from database.