setOnAction Method in JavaFX

Without creating an action, the user interface is nothing. An action is a system process that initiates when a user commands the system to perform a specific task.
In JavaFX we can add an action by using the method setOnAction()
.
In this article, we will see how we can create an action for any UI component. Also, we will see an example with an explanation to make the topic easier to understand.
Use the setOnAction
Method in JavaFX
In our below example, we just created a simple UI with two basic UI components, label
and button
. The code for our example will look like the following.
// Importing necessary packages
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.layout.HBox;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FXsetAction extends Application {
Button Btn; //Decleares a button
Label lbl; // Decleares a lebel
Scene scene; // Decleares a scent
HBox _hbox; // Decleares a Hbox
public void start (Stage PrimaryStage) throws Exception {
PrimaryStage.setTitle("JavaFX setOnAction"); // Set the application title
lbl = new Label("Button not clicked"); // Create a label with default text "No click found"
Btn = new Button("Click"); // Create a button with label "Click"
Btn.setOnAction(value -> { // Button action handler function
lbl.setText("Button Clicked!!!"); // When the button clicked, it will change the lable default text from "No click found" to "Clicked!!!".
});
_hbox = new HBox(Btn, lbl); // Create a HBox
scene = new Scene(_hbox, 400, 300); // Create a scene with HBox and necessary height and weight
PrimaryStage.setScene(scene); // Set scene
PrimaryStage.show(); // Visualize the application
}
public static void main(String[] args) {
Application.launch(args); // Launch the application
}
}
We already commented in the code on the purpose of each line. Now, let’s explain how the code works.
We created a label on our code with the default text of Button not clicked
by the line lbl = new Label ("Button not clicked");
. The purpose of this label is to show the message to the user that a key is pressed.
Then, we created a simple keypress event. As an action that will execute when the user presses a key from his keyboard, we changed the label default text and set it to Button Clicked!!!
.
So for this purpose, the code adds an action lbl.setText("Button Clicked!!!");
.
Now, when you run the code after a successful compilation, a window will open with the title JavaFX setOnAction
, and after you press a key from your keyboard, you will see an output like the one below.
Output:
Remember, if your IDE doesn’t support the automatic inclusion of libraries and packages, you may need to manually include these necessary libraries and packages before compiling.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedInRelated Article - Java JavaFX
- InvocationTargetException in JavaFX
- Play a Video Using JavaFX
- Solution to the JavaFX FXML Load Exception
- Create JavaFX Message Box
- JavaFX Media Player
- Create Square in JavaFX