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

Radio Buttons in JavaFX

Code Description:

Below is simple code for displaying a fully functional radio button and how to retrieve the selected value. Radio buttons are a lot like multiple choice tests. Only one value may be selected at a time but you can have as many choices as you want. These radio buttons must be part of a toggle group so the system knows that if one value is selected, the rest should be deselected. In the runnable example provided, we use a listener to run code whenever a change is made to the radio button values. It will then display the text of the selected radio button within the toggle group in a Label.

Radio Button Syntax:

ToggleGroup choices = new ToggleGroup();
		
RadioButton choice1 = new RadioButton("Choice 1");
choice1.setToggleGroup(choices);
	
RadioButton choice2 = new RadioButton("Choice 2");
choice2.setToggleGroup(choices);

Retrieve Selected Radio Button Syntax:

(ToggleButton)choices.getSelectedToggle()

Runnable JavaFX Radio Button Example Code:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
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("Selected: ");
		
	ToggleGroup choices = new ToggleGroup();
		
	RadioButton choice1 = new RadioButton("Choice 1");
	choice1.setToggleGroup(choices);
	
	RadioButton choice2 = new RadioButton("Choice 2");
	choice2.setToggleGroup(choices);
		
	RadioButton choice3 = new RadioButton("Choice 3");
	choice3.setToggleGroup(choices);
		
	choices.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle,Toggle new_toggle) {
		    	  
	        label.setText("Selected: " + ((ToggleButton)choices.getSelectedToggle()).getText());
		    	  
	    }	      
	});
					
	root.getChildren().addAll(choice1, choice2, choice3, label);
		
	primaryStage.setTitle("Radio 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 Radio Button Selected Output Image

Important Methods:

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

Related Links:

JavaFX Buttons JavaFX Toggle Buttons JavaFX Checkboxes