Friday, 25 May 2018

javaFX: RadioButton: select the radio button by default

By calling 'setSelected(true)' method on RadioButton object, you can make the radio button selected.

Ex
RadioButton button1 = new RadioButton();
button1.setText("Radio Button1");
button1.setSelected(true);

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);

  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);
 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment