Wednesday 9 December 2020

JavaFX: ProgressBar Demo

 

ProgressBar is a sub class of 'ProgressIndicator' which represents a horizontal bar.

 

ProgressBar class provides following constructors to get an instance of ProgressBar.

 

public ProgressBar()

Creates a new indeterminate ProgressBar.

 

public ProgressBar(double progress)

Creates a new ProgressBar with the given progress value.

 

How to set minimum height and width of ProgressBar?

progressBar.setMinHeight(30);

progressBar.setMinWidth(100);

 

How to set progress value to ProgressBar?

progressBar.setProgress(10);

 

Find the below working application.

 

ProgressBarDemo.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.ProgressBar;
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 ProgressBarDemo 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 bar");
		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);

		ProgressBar progressBar = new ProgressBar(0);
		progressBar.setMinHeight(30);
		progressBar.setMinWidth(200);

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

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

		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("ProgressBar Demo");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
}

 

Output


 

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



Previous                                                    Next                                                    Home

No comments:

Post a Comment