Monday 4 June 2018

JavaFX: Create ChoiceBox

ChoiceBox class provides below constructors to create choice box widget.

public ChoiceBox()
public ChoiceBox(ObservableList<T> items)

Ex
ChoiceBox<String> choiceBox1 = new ChoiceBox<>();
choiceBox1.setItems(FXCollections.observableArrayList("Delhi", "Bombay", "Hyderabad"));

ChoiceBox<String> choiceBox2 = new ChoiceBox<>(FXCollections.observableArrayList("Chess", "Cricket", "Tennis"));

Find the below working application.

ChoiceBoxApp.java
package com.sample.demos;

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

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ChoiceBoxApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {
  ChoiceBox<String> choiceBox1 = new ChoiceBox<>();
  choiceBox1.setItems(FXCollections.observableArrayList("Delhi", "Bombay", "Hyderabad"));

  ChoiceBox<String> choiceBox2 = new ChoiceBox<>(FXCollections.observableArrayList("Chess", "Cricket", "Tennis"));

  VBox vbox = new VBox(10, choiceBox1, choiceBox2);

  Scene scene = new Scene(vbox, 400, 300);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Choice Box 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(ChoiceBoxApp.class, args);
 }
}






Previous                                                 Next                                                 Home

No comments:

Post a Comment