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

Toggle Button in JavaFX | 100% Perfect for beginners

July 3, 2022 - Updated on December 10, 2022
in Java
Reading Time: 4 mins read
0
JavaFX Toggle Button
447
VIEWS
Share on FacebookShare on TwitterShare via Email

The toggle button in JavaFX is a button that can have the same function as a switch that can be turned on or off. The JavaFX ToggleButton has two states, selected and unselected. The selected property is true when it is in the selected state. Otherwise, it is false. RadioButton and CheckBox are also the same.

This tutorial is perfect for beginners, and you will learn how to use the JavaFX ToggleButton. I will provide examples in this tutorial from creating the toggle button to finished.

Contents

Toggle
  • How to use the Toggle Button in JavaFX
  • Add the node to the scene
    • Output
  • Setting the Toggle Button Text
    • Output
  • Setting the JavaFX ToggleButton Font
    • Output
  • Determine state
  • Toggle Button Style
    • Output
  • JavaFX toggle button example
  • YouTube Video

How to use the Toggle Button in JavaFX

Creating the toggle button is very easy to do. You simply create an object of ToggleButton and set the name. Creating the toggle button in JavaFX is the same as creating the button control. This node is used to select a choice and not to execute a command. The code snippet below will show you how to create the toggle button in JavaFX.

// Create the Toggle Button
ToggleButton tb = new ToggleButton("Turn ON");

Add the node to the scene

To display the toggle button in JavaFX, you must add it to the scene so that the user can interact with it. If you don’t know how we can add nodes to the scene, you can learn more about the layouts in JavaFX. The following example code snippet below will show you how to add the JavaFX ToggleButton to the Scene graph.

//Create the Layout
VBox root = new VBox();
// Create the scene object
Scene scene = new Scene(root, 400, 400);

// Create the Toggle Button and display the node
ToggleButton tb = new ToggleButton("Turn ON");
root.getChildren().add(tb);

Output

JavaFX Toggle Button

Setting the Toggle Button Text

It is also important to learn how to set or change the text in the toggle button in JavaFX. By doing this, you can use the setText() method. The example code below will show you how to set the text in JavaFX ToggleButton.

ToggleButton tb = new ToggleButton("Turn ON");
tb.setText("This is new Text");

Output

Toggle Button in JavaFX

Setting the JavaFX ToggleButton Font

To set the font in the toggle button in JavaFX is very easy. You create the font object and set it. You can also use SceneBuilder if you are developing your application with SceneBuilder. The example code below will show you how to set the font in your JavaFX application.

ToggleButton tb = new ToggleButton("Turn ON");
Font font = Font.font("Serif", FontWeight.EXTRA_BOLD, 30);
tb.setFont(font);

Output

JavaFX ToggleButton Font

Determine state

Use the isSelected() method to determine whether the toggle button in JavaFX is selected or not. The isSelected() method is a boolean value that is true or false. Proceed to the example code below to learn more.

ToggleButton tb = new ToggleButton("TURN ON");
Boolean isSelected = tb.isSelected();
if (isSelected) {
    tb.setText("Turn OFF");
} else {
    tb.setText("Turn ON");
}

Toggle Button Style

You can set styles in your toggle button to make your toggle button’s appearance better than the default style. You can add external CSS to your JavaFX application or use the inline style when setting styles in your nodes. The following code will show you how to add style to your node using the inline style.

ToggleButton tb = new ToggleButton("TURN ON");
tb.setStyle("-fx-text-fill: white; -fx-background-color: black;");

Output

JavaFX ToggleButton Style

JavaFX toggle button example

This example will show you the whole code and show you exactly how the toggle button works in the JavaFX application. Please proceed below to learn and apply this to your program.

package com.example.understandingtogglebutton;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

import java.io.IOException;

public class ToggleButtonExample extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        VBox root = new VBox();
        Scene scene = new Scene(root, 400, 400);

        ToggleButton tb = new ToggleButton("Turn ON");
        tb.setText("This is new Text");
        Boolean isSelected = tb.isSelected();
        Font font = Font.font("Serif", FontWeight.EXTRA_BOLD, 30);
        tb.setFont(font);
        tb.setStyle("-fx-text-fill: white; -fx-background-color: black;");
        Label lbl = new Label("The Toggle Button is UNSELECTED");
        root.getChildren().addAll(tb, lbl);
        root.setAlignment(Pos.CENTER);

        if (isSelected) {
            lbl.setText("The Toggle Button is SELECTED");
            tb.setText("Turn OFF");
        } else {
            lbl.setText("The Toggle Button is UNSELECTED");
            tb.setText("Turn ON");
        }

        tb.setOnAction(event -> {
            if (tb.isSelected()) {
                lbl.setText("The Toggle Button is SELECTED");
                tb.setText("Turn OFF");
            } else {
                lbl.setText("The Toggle Button is UNSELECTED");
                tb.setText("Turn ON");
            }
        });

        tb.setOnMouseEntered(event ->{tb.setCursor(Cursor.HAND);});

        stage.setScene(scene);
        stage.setTitle("Understanding The ToggleButton");
        stage.show();
    }

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

YouTube Video

YouTube video
Previous Post

How to use the JavaFX MenuButton | 100% Perfect Tutorial

Next Post

RadioButton in JavaFX | 100% Perfect for beginners

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
216
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
590
Next Post
RadioButton in JavaFX

RadioButton in JavaFX | 100% Perfect for beginners

How to use CheckBox in JavaFX | 100% Perfect For Beginners

How to use CheckBox in JavaFX | 100% Perfect For Beginners

How to install IntelliJ IDEA on Windows 10

How to install IntelliJ IDEA on Windows 10 and 11 | Perfect

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.