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

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

November 27, 2022
in Java
Reading Time: 5 mins read
0
Slider in JavaFX webThumb
312
VIEWS
Share on FacebookShare on TwitterShare via Email

Slider in JavaFX is the best way to use it if you want to select a numeric value from a number range by sliding the slider knob. In this tutorial, you will know how to use the slider in JavaFX. You need to set the minimum and maximum value of the slider when using it, and you have to use the double data type for its value.

Contents

Toggle
  • How to use the slider in JavaFX
    • Creating the Slider
    • Add the slider to the scene graph
      • Output
    • Setting values of the slider
    • Getting the selected value from the slider
      • Output
    • Commonly used properties in JavaFX Slider
      • Output
    • Snap to ticks
    • Styling the Slider with CSS
  • YouTube Video

How to use the slider in JavaFX

The JavaFX slider is very easy to use when you want your users to select a numeric value from a number range by just sliding it. You can simply drag or slide the slider knob or thumb to change a wanted value. Creating the slider in JavaFX is also very easy to do; we will use its constructor and instantiate it.

In this tutorial, I will walk you through how to use the slider in JavaFX. I will show you how to create, show it to the application window, and show you some really useful properties of the JavaFX slider.

Slider has major and minor tick marks which show lines of the User Interface of slider in JavaFX, and it indicates the location of values where the user’s selected numeric range. I will show an example of that later. Now, the following example below will show you how to create the slider in JavaFX.

Creating the Slider

As mentioned above, creating the slider is very easy. There are two options for you to create this node in JavaFX. You can either use the scene builder application or code it manually. If you want to use the scene builder, please go ahead to the YouTube video below to learn more because this example will show you codes on how you can create the slider.

Slider has two constructors to use when creating this node, there is a default constructor where you can create the slider by default and the other is a parameterized constructor which you can pass values to the constructor to specify its minimum, maximum and current value. Please go ahead to the example code below to learn more about how to create it.

// using the default constructor
Slider slider = new Slider();

// using the parameterized constructor
Slider slider = new Slider(0.0, 100.0, 10.0);

Add the slider to the scene graph

In this example you cannot see the slider if you don’t do this part. Adding the slider to the scene graph is pretty simple, you just need to use the layout in JavaFX. You can use any layout in JavaFX like StackPane, AnchorPane, etc. Please go ahead to the example code below to learn more about how to add the slider to the scene.

// create the slider
Slider slider = new Slider(0.0, 100.0, 0.0);

// create the layout
StackPane layout = new StackPane(slider);
//create the scene
Scene scene = new Scene(layout, 500, 500); // 500 is the scene size
// call the stage to set the scene
stage.setScene(scene);
stage.setTitle("Understanding the slider");
stage.show();

Output

How to use the slider in JavaFX

Setting values of the slider

Setting values of the slider in JavaFX is also important. If you wonder what values are in the parameterized constructor, those are called initial values. In this example, we will use the setters of the slider to set the values we need. We can set the minimum and maximum value range of the slider, and we can also set the knob or thumb value of the slider by calling the setValue() method. Once you are familiar with it, not only are these methods or properties you can set, there are a lot of properties in JavaFX Slider you can too.

// setting values
slider.setMin(0.0);
slider.setMax(100.0);
slier.setValue(5.0);

Getting the selected value from the slider

Most of the time when we have nodes in our JavaFX application, we need to do an action and get the value from the action performed. In this JavaFX slider example, we need to use the Slider valueProperty() method and also addListener() method to get the value. Please go ahead below and see how to get the selected value from the slider.

slider.valueProperty().addListener((ObservableValue<? extends Number> num, Number oldVal, Number newVal)->{
    float value = Float.parseFloat(String.format("%.1f",newVal));

    if(value <= 37.2){
        temp.setText("Body Temperature: "+value+"\n"+"Normal Body Temperature");
    }else if(value >= 37.3){
        temp.setText("Body Temperature: "+value+"\n"+"You have a fever");
    }
});

The temp is an object of a JavaFX Label.

Output

Slider in JavaFX Value

Commonly used properties in JavaFX Slider

These properties are the most commonly used properties in the slider because they are useful to the user’s end to show the tick marks and the label. As mentioned above, tick marks are the lines that specify the distance. The tick labels are also the numbers as shown in the major ticks. The example code below shows how to show and hide these properties in the slider in JavaFX.

slider.setShowTickLabels(true);
slider.setShowTickMarks(true);

Set the boolean value to false if you want to hide the tick marks and labels.

Output

Slider in JavaFX Ticks and Labels

Snap to ticks

This property is one of the most commonly used properties in the slider in JavaFX. If you enable this property, you will see a magnetic effect when you drag the thumb or the slider knob. Please see the example code below to learn more about how to enable this feature in your JavaFX slider.

slider.setSnapToTicks(true); // set to false if you want to disable this

Styling the Slider with CSS

JavaFX CSS is very useful to make your JavaFX application looks great. The slider has these CSS properties as shown below.

  • -fx-orientation
  • -fx-show-tick-labels
  • -fx-show-tick-marks
  • -fx-major-tick-unit
  • -fx-minor-tick-count
  • -fx-show-tick-labels
  • -fx-snap-to-ticks
  • -fx-block-increment

Slider also supports CSS pseudo-classes. Check the substructures below that can be styled.

  • axis
  • track
  • thumb
.slider > .axis {
 -fx-tick-label-fill: blue;
 -fx-tick-length: 15px;
 -fx-minor-tick-length: 5px
}
.slider > .axis > .axis-tick-mark {
 -fx-stroke: red;
}
.slider > .axis > .axis-minor-tick-mark {
 -fx-stroke: green;
}

.slider > .track {
 -fx-background-color: red;
}

.slider .thumb {
 -fx-background-radius: 0;
}

.slider .thumb {
 -fx-background-image: url("thumb.jpg");
}

/* An inverted triangle */
.slider > .thumb {
 -fx-shape: "M0, 0L10, 0L5, 10 Z";
}
/* A triangle pointing to the right*/
.slider:vertical > .thumb {
 -fx-shape: "M0, 0L10, 5L0, 10 Z";
}

/* An inverted triangle below a rectangle*/
.slider > .thumb {
 -fx-shape: "M0, 0L10, 0L10, 5L5, 10L0, 5 Z";
}
/* A triangle pointing to the right by the right side of a rectangle */
.slider:vertical > .thumb {
 -fx-shape: "M0, 0L5, 0L10, 5L5, 10L0, 10 Z";
}

YouTube Video

YouTube video
Previous Post

How to use the JavaFX SplitPane | 100% Perfect Tutorial

Next Post

How to use the Menu Bar 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
JavaFX MenuBar

How to use the Menu Bar in JavaFX | 100% Perfect Tutorial

JavaFX Context Menu

How to use the JavaFX ContextMenu | 100% Perfect Tutorial

How to use the JavaFX ToolBar

How to use the JavaFX ToolBar | 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.