Tuesday, 8 December 2020

JavaFX: ProgressIndicator

 

ProgressIndicator

ProgressIndicator is a circular controller that indicates progress. Progress status can be indeterminate or finite. If the status is ‘indeterminate’ that mean application is unable to determine the task completion time before hand.

 

‘finite’ means, progress time is known beforehand and can be expressed in percentages.

 

How to define ProgressIndicator?

ProgressIndicator class provides following constructors to define ProgressIndicator instance.

 

public ProgressIndicator()

public ProgressIndicator(double progress)

Creates a new ProgressIndicator with the given progress value.

 

How to set minimum height and minimum width of ProgressIndicator?

Use 'setMinHeight' and 'setMinWidth' methods.

 

progressIndicator.setMinHeight(300);

progressIndicator.setMinWidth(300);

 

How to set the progress of ProgressIndicator?

Use ‘setProgress’ method.

progressIndicator.setProgress(5.0);

 

Find the below working application

 

ProgressIndicatorDemo.java

package com.sample.app.widgets;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.Slider;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.stage.Stage;

public class ProgressIndicatorDemo extends Application {

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

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		HBox hBox1 = new HBox();
		Label label1 = new Label("Move slider, same will show in progress indicator");
		label1.setFont(Font.font("Verdana", FontPosture.REGULAR, 20));
		hBox1.getChildren().add(label1);
		hBox1.setPadding(new Insets(20, 20, 20, 20));

		HBox hBox2 = new HBox();
		hBox2.setPadding(new Insets(20, 20, 20, 20));
		hBox2.setSpacing(30);

		Slider slider = new Slider(0, 1, 0);
		slider.setMinHeight(20);
		slider.setMinWidth(50);
		slider.setStyle("-fx-background-color:paleblue");
		slider.setMinSize(400, 30);
		slider.setMajorTickUnit(0.2);
		slider.setShowTickLabels(true);
		slider.setShowTickMarks(true);

		ProgressIndicator progressIndicator = new ProgressIndicator(0);
		progressIndicator.setMinHeight(300);
		progressIndicator.setMinWidth(300);

		slider.valueProperty().addListener((ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> {
			progressIndicator.setProgress((Double) new_val);
		});

		hBox2.getChildren().addAll(slider, progressIndicator);

		DropShadow dropShadow = new DropShadow();
		dropShadow.setOffsetX(5);
		dropShadow.setOffsetY(5);
		dropShadow.setColor(Color.GRAY);

		VBox vBox = new VBox();
		vBox.getChildren().addAll(hBox1, hBox2);
		vBox.setEffect(dropShadow);

		Scene scene = new Scene(vBox, 800, 500, Color.WHITE);

		primaryStage.setTitle("ProgressIndicator Demo");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
}

 

Output



Change the slider position, you can observe same is reflected in ProgressIndicator.


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment