Thursday 7 June 2018

JavaFX: Set the behavior to choice box

By adding change listener to ChoiceBox, you can set the behavior to choice box.

Ex
choiceBox1.getSelectionModel().selectedIndexProperty()
.addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
         if ((int) oldValue == -1) {
                  System.out.println("Selection changed form none to " + observableList.get((int) newValue));
         } else {
                  System.out.println("Selection changed form " + observableList.get((int) oldValue) + " to "
                                    + observableList.get((int) newValue));
         }

});

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.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Separator;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.collections.ObservableList;

public class ChoiceBoxApp extends Application {
 private static final Separator SEPARATOR = new Separator();

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {
  ChoiceBox<Object> choiceBox1 = new ChoiceBox<>();
  choiceBox1.setTooltip(new Tooltip("Select the language"));
  ObservableList<Object> observableList = FXCollections.observableArrayList("New", "Open File...", SEPARATOR,
    "close", "close all", SEPARATOR, "save", "save as", "save all");

  choiceBox1.setItems(observableList);

  choiceBox1.getSelectionModel().selectedIndexProperty()
    .addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
     if ((int) oldValue == -1) {
      System.out.println("Selection changed form none to " + observableList.get((int) newValue));
     } else {
      System.out.println("Selection changed form " + observableList.get((int) oldValue) + " to "
        + observableList.get((int) newValue));
     }

    });

  VBox vbox = new VBox(10, choiceBox1);

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




Try to select different options, you can able to see below kind of messges.

Selection changed form none to Open File...
Selection changed form Open File... to save
Selection changed form save to save all
Selection changed form save all to close all


Previous                                                 Next                                                 Home

No comments:

Post a Comment