JavaFX example to set slider to the progress bar



ProgressBar − A progress bar is an indicator of the advancement of an event (a series of steps). You can create a progress bar by instantiating the javafx.scene.control.ProgressBar class.

Slider − JavaFX provides a class known as Slider, this represents a slider component that displays a continuous range of values. This contains a track on which the numerical values are displayed. Along the track, there is a thumb pointing to the numbers. You can provide the maximum, minimum, and initial values of the slider.

Example

In the following example, we are setting the slider to the ProgressBar.

import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ProgressBar; import javafx.scene.control.ProgressIndicator; import javafx.scene.control.Slider; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class ProgressBar_Slider extends Application {    public void start(Stage stage) {       //Creating a progress bar       ProgressBar progress = new ProgressBar(0.1);       //Creating a progress indicator       ProgressIndicator indicator = new ProgressIndicator(0.1);       //Setting the size of the progress bar       progress.setPrefSize(200, 30);       //Creating a slider       Slider slider= new Slider(0, 1, 0);       slider.setShowTickLabels(true);       slider.setShowTickMarks(true);       slider.setMajorTickUnit(0.1);       slider.setBlockIncrement(0.1);       slider.valueProperty().addListener(new ChangeListener<Number>() {          public void changed(ObservableValue <?extends Number>observable, Number          oldValue, Number newValue){             //Setting the angle for the rotation             progress.setProgress((double) newValue);             //Setting value to the indicator             indicator.setProgress((double) newValue);          }       });       //Creating a hbox to hold the progress bar and progress indicator       HBox hbox = new HBox(50);       hbox.setPadding(new Insets(75, 150, 50, 60));       hbox.getChildren().addAll(slider, progress, indicator);       //Setting the stage       Group root = new Group(hbox);       Scene scene = new Scene(root, 595, 200, Color.BEIGE);       stage.setTitle("Progress Bar");       stage.setScene(scene);       stage.show();    }    public static void main(String args[]){       launch(args);    } }

Output

Updated on: 2020-05-18T08:12:42+05:30

486 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements