How to set action to a RadioButton in JavaFX?

JavaFXObject Oriented ProgrammingProgramming

A radio button is a type of button, which is circular in shape. It has two states, selected and deselected. Generally, radio buttons are grouped using toggle groups, where you can only select one of them.

You can create a radio button in JavaFX by instantiating the javafx.scene.control.RadioButton class, which is the subclass of the ToggleButton class. Action is generated whenever a radio button is pressed or released. You can set a radio button to a group using the setToggleGroup() method.

Setting Action to a RadioButton −

The property named selected of the RadioButton class specifies whether the current checkbox is selected. Its value will be true if checked and false if unchecked.

The selectedProperty() method returns a boolean property indicating whether the current checkbox is checked. If you want to perform a certain actions in case a checkbox is selected. You can add a listener to this property as −

checkBox2.selectedProperty().addListener(
   //. . . . .
);

Example

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Slider;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class RadioButton_Action extends Application {
   public void start(Stage stage) {
      //Creating a button
      Button button = new Button("Hi");
      button.setPrefSize(40, 40);
      //Creating a slider to resize the button
      Slider slider = new Slider(40, 200, 40);
      //Setting its orientation to Horizontal
      slider.setPrefHeight(180);
      slider.setOrientation(Orientation.VERTICAL);
      slider.setShowTickLabels(true);
      slider.setShowTickMarks(true);
      slider.setMajorTickUnit(40);
      slider.setBlockIncrement(20);
      slider.valueProperty().addListener(new ChangeListener<Number>() {
         public void changed(ObservableValue <?extends Number>observable, Number
         oldValue, Number newValue){
            button.setPrefSize((double)newValue, (double)newValue);
         }
      });
      RadioButton prevent = new RadioButton();
      RadioButton reset = new RadioButton();
      ToggleGroup group = new ToggleGroup();
      prevent.setToggleGroup(group);
      reset.setToggleGroup(group);
      prevent.selectedProperty().addListener(
      (ObservableValue<? extends Boolean> ov, Boolean old_val, Boolean new_val) ->{
         button.setMinWidth(40);
         button.setPrefWidth(40);
         button.setMaxWidth(40);
         button.setMinHeight(40);
         button.setMaxHeight(40);
         button.setPrefHeight(40);
      });
      reset.selectedProperty().addListener(
      ObservableValue<? extends Boolean> ov, Boolean old_val, Boolean new_val) -> {
         button.setPrefHeight(40);
         button.setPrefWidth(40);
         button.setMinWidth(Button.USE_PREF_SIZE);
         button.setMaxWidth(Button.USE_PREF_SIZE);
         button.setMinHeight(Button.USE_PREF_SIZE);
         button.setMaxHeight(Button.USE_PREF_SIZE);
      });
      //Creating the pane
      BorderPane pane = new BorderPane();
      pane.setCenter(button);
      pane.setRight(new HBox(new Label("Prevent Reize"), prevent, new Label("Allow Resize"), reset));
      pane.setLeft(new VBox(new Label("Button Reize"), slider));
      pane.setPadding(new Insets(10, 10, 10, 10));
      //Preparing the scene
      Scene scene = new Scene(pane, 595, 250);
      stage.setTitle("JavaFX Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

raja
Published on 16-May-2020 07:37:34
Advertisements