Sunday, 29 November 2020

JavaFX: RadioButton

 

Radio button either selected or in deselected state. RadioButtons create a series of items where only one item can be selected.

 

Can a radio button generate event?

Yes, selecting or deselecting a radio button generates an action event.

 

When you place radio buttons in a toggle group, the behaviour is mutually exclusive. At a time, only one radio button in the group is selected. A RadioButton that is not in a ToggleGroup can be selected and unselected.  By default a RadioButton is not in a ToggleGroup.

 

RadioButton class provides following constructors to define a RadioButton.

 

public RadioButton()

public RadioButton(String text)

'text' specifies a text string for its label.

 

Example

RadioButton maleRadioButton = new RadioButton("Male");

maleRadioButton.setAlignment(Pos.CENTER);

maleRadioButton.setFont(Font.font("Verdana", FontPosture.REGULAR, 30));

maleRadioButton.setPadding(new Insets(10, 10, 10, 10));

 

RadioButton femaleRadioButton = new RadioButton("Female");

femaleRadioButton.setAlignment(Pos.CENTER);

femaleRadioButton.setFont(Font.font("Verdana", FontPosture.REGULAR, 30));

femaleRadioButton.setPadding(new Insets(10, 10, 10, 10));

 

How to add event handling to radio buttons?

Using ‘setOnAction’ method, you can add event handling.

maleRadioButton.setOnAction((event) -> {
	Alert alert = new Alert(AlertType.INFORMATION);
	alert.setTitle("Mr....");
	alert.setHeaderText("Hello!!!!");
	alert.setContentText("Male button selected");

	alert.showAndWait();
});

femaleRadioButton.setOnAction((event) -> {
	Alert alert = new Alert(AlertType.INFORMATION);
	alert.setTitle("Madam....");
	alert.setHeaderText("Hello!!!!");
	alert.setContentText("Female button selected");

	alert.showAndWait();
});

 

How to add radio buttons to ToggleGroup?

ToggleGroup toggleGroup = new ToggleGroup();

toggleGroup.getToggles().addAll(maleRadioButton, femaleRadioButton);

 

Find the below working application.

 

RadioButtonDemo.java

package com.sample.app.widgets;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.stage.Stage;

public class RadioButtonDemo extends Application {

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

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		HBox hBox1 = new HBox();
		Label label1 = new Label("Select your gender");
		label1.setFont(Font.font("Verdana", FontPosture.REGULAR, 45));
		hBox1.getChildren().add(label1);
		hBox1.setPadding(new Insets(20, 20, 20, 20));

		RadioButton maleRadioButton = new RadioButton("Male");
		maleRadioButton.setAlignment(Pos.CENTER);
		maleRadioButton.setFont(Font.font("Verdana", FontPosture.REGULAR, 30));
		maleRadioButton.setPadding(new Insets(10, 10, 10, 10));

		RadioButton femaleRadioButton = new RadioButton("Female");
		femaleRadioButton.setAlignment(Pos.CENTER);
		femaleRadioButton.setFont(Font.font("Verdana", FontPosture.REGULAR, 30));
		femaleRadioButton.setPadding(new Insets(10, 10, 10, 10));
		
		maleRadioButton.setOnAction((event) -> {
			Alert alert = new Alert(AlertType.INFORMATION);
			alert.setTitle("Mr....");
			alert.setHeaderText("Hello!!!!");
			alert.setContentText("Male button selected");

			alert.showAndWait();
		});
		
		femaleRadioButton.setOnAction((event) -> {
			Alert alert = new Alert(AlertType.INFORMATION);
			alert.setTitle("Madam....");
			alert.setHeaderText("Hello!!!!");
			alert.setContentText("Female button selected");

			alert.showAndWait();
		});

		ToggleGroup toggleGroup = new ToggleGroup();
		toggleGroup.getToggles().addAll(maleRadioButton, femaleRadioButton);

		HBox hBox2 = new HBox();
		hBox2.setSpacing(10);
		hBox2.setPadding(new Insets(20, 20, 20, 20));
		hBox2.getChildren().addAll(maleRadioButton, femaleRadioButton);

		DropShadow dropShadow = new DropShadow();
		dropShadow.setOffsetX(5);
		dropShadow.setOffsetY(5);
		dropShadow.setColor(Color.GRAY);

		VBox vBox = new VBox();
		vBox.getChildren().addAll(hBox1, hBox2);
		vBox.setEffect(dropShadow);
		vBox.setStyle("-fx-background-color:lightyellow");

		Scene scene = new Scene(vBox, 500, 500, Color.LIGHTYELLOW);

		primaryStage.setTitle("Radio Butto Demo1");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

 

Output

 

 


Depends on your selection of radio button, you will see respective alert box.



 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment