How to show ProgressIndicator at Pane Center in Javafx

I have a panel with some controls and a button. When I click on the button, I want to show the ProgressIndicator in the center of the panel without deleting any controls.

When I add a ProgressIndicator to the panel during the onAction button, it adds it under the button. I want it to overlap the panel.

The following figure explains what I want.

progress indicator

the code

 package fx; import javafx.application.Application; 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.ProgressIndicator; import javafx.scene.control.TextField; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage arg0) throws Exception { final VBox bx = new VBox(); bx.setAlignment(Pos.CENTER); TextField userName = new TextField("User Name"); userName.setMaxWidth(200); TextField email = new TextField("Email"); email.setMaxWidth(200); Button submit = new Button("Submit"); submit.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { ProgressIndicator pi = new ProgressIndicator(); //adding here but it is adding at below of button //how to do here bx.getChildren().add(pi); //Further process } }); bx.getChildren().addAll(userName, email, submit); Scene c = new Scene(bx); arg0.setScene(c); arg0.setMinWidth(500); arg0.setMinHeight(500); arg0.show(); } public static void main(String[] args) { Main h = new Main(); h.launch(args); } } 
+7
javafx javafx-8
source share
1 answer

You need to use StackPane as the root layout instead of using VBox. StackPane allows you to stack vertices on top of each other (z-order).

In the button action, you can create a new ProgressIndicator and add it to your StackPane. I introduced another VBox as a parent for the indicator, because I did not want the indicator to fix all available space. You can disable existing VBox, to get the effect of greying on the button action after the process is complete, you can turn on the VBox again.

enter image description here


 import javafx.application.Application; 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.ProgressIndicator; import javafx.scene.control.TextField; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage arg0) throws Exception { StackPane root = new StackPane(); VBox bx = new VBox(); bx.setAlignment(Pos.CENTER); TextField userName = new TextField("User Name"); userName.setMaxWidth(200); TextField email = new TextField("Email"); email.setMaxWidth(200); Button submit = new Button("Submit"); submit.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { ProgressIndicator pi = new ProgressIndicator(); VBox box = new VBox(pi); box.setAlignment(Pos.CENTER); // Grey Background bx.setDisable(true); root.getChildren().add(box); } }); bx.getChildren().addAll(userName, email, submit); root.getChildren().add(bx); Scene c = new Scene(root); arg0.setScene(c); arg0.setMinWidth(500); arg0.setMinHeight(500); arg0.show(); } public static void main(String[] args) { launch(args); } } 
+12
source share

All Articles