FreeCode

Donate HTML/CSS JavaFX Java Home
JavaFX Starter Template JavaFX Labels JavaFX Buttons JavaFX Toggle Buttons JavaFX Radio Buttons and Toggle Groups JavaFX Checkboxes JavaFX Choice Boxes JavaFX Combo Boxes JavaFX Text Areas JavaFX Text Fields JavaFX List Views JavaFX Image Views JavaFX Color Pickers JavaFX Date Pickers JavaFX Resizable Split Pane Layouts JavaFX VBox (Vertical Box Layouts) JavaFX HBox (Horizontal Box Layouts) JavaFX Flow Pane Layouts JavaFX Tile Pane Layouts JavaFX Web Views

JavaFX Buttons

Code Description:

Buttons are used to execute a certain section of code much like a normal Method but upon a click from the user on the user interface. The code below displays a fully functional example button that performs an action when clicked or pressed.

Button Syntax:

Button button = new Button(“Button Text Goes Here“);
		
button.setOnAction(new EventHandler<ActionEvent>() {

     @Override
     public void handle(ActionEvent event) {
				
	  // Add Button Actions Here

     }
});

Runnable JavaFX Button Example Code:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Demo extends Application {

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

     @Override
     public void start(Stage primaryStage) throws Exception {
		
          VBox root = new VBox();
		
	  Label label = new Label("");
		
	  Button button = new Button("Press Me!");
		
	  button.setOnAction(new EventHandler<ActionEvent>() {
			
	       @Override
		public void handle(ActionEvent event) {
				
		     label.setText("The button was pressed!");
				
		}
	  });
		
	  root.getChildren().add(button);
	  root.getChildren().add(label);
		
	  primaryStage.setTitle("JavaFX Button Demo");
	  primaryStage.setScene(new Scene(root, 200, 100));
	  primaryStage.show();
		
     }

}

How to Run This Code:

Make sure that your Class name is the same as ours. In this case, we are using the Class name of "Demo". Alternatively, you can change the Class name in the provided code to match yours.

Note: You will NOT be able to copy and paste this code into your IDE unless you first set up JavaFX for your project. Check out this link if you need help setting up JavaFX:

JavaFX Comprehensive Setup Guide

Output:

JavaFX Button Application Output Image

Important Methods:

  • fire() - Used to programatically "press" a button without any user interaction. Use this if you need to call the code in the event handler.
  • getText() - Used to get the text displayed on the button.
  • setDisable() - Used to make the button grayed out and unclickable. EX: "button.setDisable(true);"
  • setFont() - Set the font and font size for the button text. EX: "button.setFont(new Font("Courier New", 20));"
  • setPrefSize() - Set the preferred width and height for the button.
  • setOnAction() - Set the code and event handler for when the button is clicked or pressed.
  • setText() - Set the text displayed on the button.
  • setStyle() - Set an inline CSS-like style on the button. EX: "button.setStyle("-fx-background-color: green;");"
  • setTooltip() - Set a message that displays when a user hovers over the button control for a certain amount of time. EX: "button.setTooltip(new Tooltip("Press Me"));"
  • setVisible() - Show or hide a button from the scene. EX: "button.setVisible(false);"

Related Links:

JavaFX Toggle Buttons JavaFX Radio Buttons and Toggle Groups JavaFX Checkboxes