Saturday 26 May 2018

JavaFX: How to check whether radio button is selected or not

By calling the method 'isSelected' on RadioButton object, we can check whether radio button is selected or not.

Ex
RadioButton button1 = new RadioButton();
button1.setText("Radio Button1");
button1.setSelected(true);
System.out.println("Is radio button selected : " + button1.isSelected());

Find the below working application.

RadioButtonApp.java
package com.sample.demos;

import static javafx.geometry.Pos.CENTER;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class RadioButtonApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {

  RadioButton button1 = new RadioButton();
  button1.setText("Radio Button1");
  button1.setSelected(true);
  System.out.println("Is radio button selected : " + button1.isSelected());
  
  VBox vBox = new VBox(10, button1);
  vBox.setAlignment(CENTER);

  Scene scene = new Scene(vBox, 400, 400);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Radio Button Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java

package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(RadioButtonApp.class, args);
 }
}

When you ran above application, you can able to see the output "Is radio button selected : true" in console.


Previous                                                 Next                                                 Home

No comments:

Post a Comment