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

JavaFX Button with examples | 100% Perfect for beginners

May 14, 2022 - Updated on December 10, 2022
in Java
Reading Time: 6 mins read
2
JavaFX Button
633
VIEWS
Share on FacebookShare on TwitterShare via Email

Working with the JavaFX button is very easy, and it is one of the first steps in learning JavaFX programming. The button plays a very important role in your application. There are three types of controls in JavaFX that represent buttons. These are “buttons to execute commands, buttons to make choices, and buttons to execute commands as well as make choices.”

You will learn how to use the JavaFX Button. So, a button can be activated in different ways, for example, by clicking it using a mouse, a mnemonic, an accelerator key, or other combinations. When the button is activated, it will execute a command known as a command button.

Examples of buttons to make choices are ToggleButton, CheckBox, and RadioButton.

Contents

Toggle
  • JavaFX Button Tutorial with examples
    • Creating a button in JavaFX
      • Normal Button Example
      • Default Button Example
      • Cancel Button Example
    • Add the button to a Scene Graph Example
    • JavaFX Button Getter and Setter Example
      • Output
    • JavaFX Button Text Wrapping Example
      • Output
    • Adding an image to a button Example
      • Output
    • Adding Styles to a Button Example
      • Output
    • Enable and Disable a Button Example
    • Button Event Example
  • YouTube Video

JavaFX Button Tutorial with examples

We will learn the essential uses of the JavaFX Button, and we will go over the basic examples of Buttons in JavaFX, like creating the button, Getters, and Setters, adding styles, and more. The first step we are going to do is to create the button in JavaFX. Please proceed below to learn more.

Creating a button in JavaFX

Creating a button in JavaFX is very simple and easy. We need to create a button object and add a label or a button name. A button can work in one of 3 ways. This can be a normal, default, or cancel button.

A normal button is activated when the ActionEvent is fired. At the same time, the default button is activated when the Enter key on the keyboard is pressed, but no other node in the scene consumes the Enter key event, and when the Esc key is pressed, the cancel button is activated, and no other node consumes the Esc key event.

Normal Button Example

Below is an example of a normal button. Let’s proceed and make the JavaFX Button with an action event.

// Normal Button
Button normalButton = new Button("Normal Button");
normalButton.setOnAction(e -> function());

Default Button Example

Below is an example of a default button. Let’s proceed and make an action event.

// Default Button
Button defaultButton = new Button("Default Button");
defaultButton.setDefaultButton(true); // Make it a default button
defaultButton.setOnAction(e -> function());

Cancel Button Example

Below is an example of a cancel button. Let’s proceed and make an action event.

// Cancel button
Button cancelButton = new Button("Cancel Button");
cancelButton.setCancelButton(true);
cancelButton.setOnAction(e -> function());

Add the button to a Scene Graph Example

To make our button visible to the users, we need to add the button to the Scene Graph, and we have two options for adding the button. The button can be directly added to the scene or a layout in JavaFX. Click the link to learn more, and proceed below to learn how to add the button to a Scene Graph.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 *
 * @author KENSOFTPH.COM
 */

public class UnderstandingButton extends Application {
    
    @Override
    public void start(Stage stage) {
        
        // Adding the button with layout pane
        AnchorPane root = new AnchorPane();
        Button btn = new Button("Button");
        root.getChildren().add(btn);
        Scene scene = new Scene(root);
        
        // Or adding the button directly to the Scene
        Button btn = new Button("Button");
        Scene scene = new Scene(btn);
        
        stage.setScene(scene);
        stage.setTitle("Understanding Button");
        stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

JavaFX Button Getter and Setter Example

Using the JavaFX Button getter and setter is very easy. You need to use these functions if you want to set the label of the button or rename the existing label of the button. On the other hand, the getter function is used to get the label or the name of the button. Please proceed below to learn more.

// Setter
Button btn = new Button();
btn.setText("Setting text");

// Getter
Button btn = new Button("Getting Text");
Label lbl = new Label("Rename");
btn.setOnAction(e ->{ lbl.setText(btn.getText()); });

Output

JavaFX Button getter and setter

JavaFX Button Text Wrapping Example

Text wrapping is also possible on the JavaFX button. Enabling the text wrapping will help you read the text if the text is longer than the button and the text will break into multiple lines until the text is visible. To set the text wrapping, you only need to use the setWrapText() method and set the parameter to true.

Button btn = new Button("This is a long text");
btn.setWrapText(true);

Output

JavaFX Button Text Wrapping

Adding an image to a button Example

You can also add an image inside the button. Adding an image to a button is also a way to describe and make it more beautiful. To add an image inside the button, we need to use the ImageView. ImageView is also a node used for painting images. Please proceed below to learn more.

Button btn = new Button("Button");
ImageView image = new ImageView("/res/sample.png");
btn.setGraphic(image);

Output

JavaFX Button with Image

Adding Styles to a Button Example

JavaFX Button has a feature to add styles or CSS in JavaFX. You can easily add an inline style or external CSS to your JavaFX application. If you are unfamiliar with JavaFX CSS, click the link to learn more. CSS is very important if you want to make your application beautiful. We will not go further with the JavaFX CSS, but click the given link to proceed if you want to learn more. Also, please move below to learn how to add an inline style to the JavaFX button.

Button btn = new Button("Button");
btn.seStyle("-fx-background-color: black; -fx-text-fill: white;");

Output

JavaFX Button with Style

Enable and Disable a Button Example

If you want a feature in your application that can disable and enable the JavaFX Button. The setDisable() method is the answer to disable the button. You need to write true or false in the parameter to function it.

// To disable the button
Button btn = new Button("Button");
btn.setDisable(true);

// To enable the button
btn.setDisable(false);

Button Event Example

The JavaFX Button Event is fired or executed when the button is activated through clicking using the mouse or other methods to activate the button. To create an event, we need to use the Anonymous class or the conventional method using the Lambda expression. The Button in JavaFX has many EventHandler, and we can also use them based on our requirements.

We can also use these EventHandler in our application. The MouseClicked, MouseDragged, MouseEntered, MouseExited, MouseMoved, MousePressed, Mouse Released, and more. You may use Scene Builder for your convenience, and in this example, we will use the Action Event. Please proceed below to learn more.

Button btn = new Button("Button");
// Using the Anonymous Class
btn.setOnAction(new EventHandler(){
    @Override
    public void handle(Event event) {
        //Do some magic here
    }
    
});

// Using the Lambda Expression
btn.setOnAction(event ->(
        //Do some magic here
));

YouTube Video

YouTube video

Previous Post

StackPane JavaFX Tutorial | 100% Perfect for beginners

Next Post

JavaFX Label Tutorial | 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
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 Label Tutorial

JavaFX Label Tutorial | 100% Perfect for beginners

How to use the JavaFX Hyperlink | 100% Perfect for beginner

How to use the JavaFX Hyperlink | 100% Perfect for beginner

JavaFX MenuButton

How to use the JavaFX MenuButton | 100% Perfect Tutorial

Comments 2

  1. Avatar of Max de Argentina Max de Argentina says:
    3 years ago

    very good tutorial. You can do some with the jfoenix-9.0.10 library, which adds material design functionality to buttons and other elements.

    Reply
    • Avatar of KENSOFT KENSOFT says:
      3 years ago

      Thank you, yes I will do some jfoenix tutorials next time.

      Reply

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.