Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

ProgressBar in JavaFX with examples | 100% Free for beginner

September 25, 2022 - Updated on October 31, 2022
in Java
Reading Time: 3 mins read
0
JavaFX ProgressBar Website thumbnail
764
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • How to use the ProgressBar in JavaFX
  • JavaFX ProgressBar examples
    • Create the Progress bar and Progress indicator
    • Add the JavaFX ProgressBar and JavaFX ProgressIndicator to the Scene Graph
      • Output
    • JavaFX Task Progress Bar Example
      • Output
    • Styling the node with CSS
  • YouTube Video

How to use the ProgressBar in JavaFX

The ProgressBar in JavaFX is very useful for users if you care about impatient users. This control or node is used to give the user visual feedback about the progress of a specific task, something like downloading, loading the application, or transferring a file. If your application does not have a progress bar, it confuses the user.

JavaFX ProgressBar and JavaFX ProgressIndicator work the same, so I will include the ProgressIndicator here in this tutorial. The difference between the two nodes is that the progress Indicator shows a circle progress while the progress bar shows horizontal progress, and there are two types of state or properties in these nodes:

  • indeterminate state
  • and determinate state.

The indeterminate state works if the system cannot determine the progress of the task and, on the other side, the determinate state can determine the progress of the task and indicates the progress from 0 to 100%. So in this tutorial, you will learn how to use the progress bar in JavaFX and I will walk you through creating the node, adding the node to the scene graph, and show you how to use the progress bar and progress indicator in JavaFX.

JavaFX ProgressBar examples

The first example will be creating the progress bar and other examples to show you and learn more about the progress bar in JavaFX.

Create the Progress bar and Progress indicator

Creating these nodes is very easy. You need to create an instance of its constructor. The following example code below will show you how to create an instance of the progress bar and progress indicator.

// Create an indeterminate state progress indicator and progress bar
ProgressIndicator pi = new ProgressIndicator();
ProgressBar pb = new ProgressBar();

// Create a determinate state progress indicator and progress bar with 70% progress
// Note: the value is a double data type
ProgressIndicator pi = new ProgressIndicator(0.70);
ProgressBar pb = new ProgressBar(0.70);

Add the JavaFX ProgressBar and JavaFX ProgressIndicator to the Scene Graph

Once you created the node manually, meaning you coded to make the progress bar, you need to use the layout in JavaFX. If you are using the Scene Builder you need to use the FXMLLoader class to load the FXML file. The example code below will show you how to add or display the node to the scene graph.

// create a progress bar
ProgressBar pb = new ProgressBar(0);

// Create a button
Button btn = new Button("Make Progress");

// create a layout
VBox layout = new VBox();
layout.getChildren().addAll(pb, btn);

// create the scene
Scene scene = new Scene(layout, 400, 400);
stage.setScene(scene);
stage.show();

Output

JavaFX ProgressBar and JavaFX ProgressIndicator

JavaFX Task Progress Bar Example

In this example, you will learn how to make progress to the ProgressBar in JavaFX.

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;

public class ProgressController {

    @FXML
    private Label labelProgress;

    @FXML
    private ProgressBar progressBar;

    @FXML
    private ProgressIndicator progressIndicator;

    @FXML
    void makeProgress(ActionEvent event) {
        progress(progressIndicator);
        progress(progressBar);
    }

    private void progress(ProgressIndicator p){
        double value = p.getProgress();
        if(value < 0){
            value = 0.1;
        }else{
            value = value + 0.1;
            if(value >= 1.0){
                value = 1.0;
            }
        }
        p.setProgress(value);
        labelProgress.setText("Progress: " + Integer.toString((int) Math.round(value * 100))+ "%");
    }

}

Output

ProgressBar in JavaFX and ProgressIndicator in JavaFX

Styling the node with CSS

JavaFX CSS is almost the same as CSS for web, and it is also easy to use especially if you have a CSS background. In our Progress Bar and Progress Indicator we can apply a style using JavaFX CSS. The following CSS code below will show you the basic styling for the progress bar and progress indicator.

/* This is for progress indicator */
.progress-indicator:indeterminate {
    -fx-progress-color: red;
}
.progress-indicator:determinate {
    -fx-progress-color: blue;
}

/* This is for progress bar */
.progress-bar .track {
 -fx-background-color: lightgray;
 -fx-background-radius: 5;
}
.progress-bar .bar {
 -fx-background-color: blue;
 -fx-background-radius: 5;
}

YouTube Video

YouTube video
Previous Post

6 examples of Text Area in JavaFX | Perfect for beginners

Next Post

How to use the TitledPane in JavaFX | 100% Perfect tutorial

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
215
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
586
Next Post
How to use the TitledPane in JavaFX | 100% Perfect tutorial

How to use the TitledPane in JavaFX | 100% Perfect tutorial

JavaFX Accordion

How to use the Accordion in JavaFX | 100% Perfect Tutorial

JavaFX Pagination

How to use the JavaFX Pagination | 100% Perfect Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

    How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.