betacode

Руководство JavaFX ChoiceBox

  1. JavaFX ChoiceBox
  2. Пример ChoiceBox
  3. ChoiceBox и событие

1. JavaFX ChoiceBox

2. Пример ChoiceBox

ChoiceBoxDemo.java
package org.o7planning.javafx.choicebox;

import org.o7planning.javafx.model.Language;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class ChoiceBoxDemo extends Application {

    @Override
    public void start(Stage primaryStage) {

        Language vietnamese = new Language("vi", "Vietnamese");
        Language english = new Language("en", "English");
        Language russian = new Language("ru", "Russian");

        FlowPane root = new FlowPane();

        Label label = new Label("Select Language:");

        ObservableList<Language> languages //
                = FXCollections.observableArrayList(vietnamese, english, russian);

        ChoiceBox<Language> choiceBox = new ChoiceBox<Language>(languages);

        root.setPadding(new Insets(10));

        root.getChildren().addAll(label, choiceBox);
        root.setPadding(new Insets(10));
        root.setHgap(10);

        primaryStage.setTitle("ChoiceBox (o7planning.org)");
        Scene scene = new Scene(root, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}
Language.java
package org.o7planning.javafx.model;

public class Language {

    private String code;
    private String name;
    private String greeting;

    public Language() {

    }

    public Language(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public Language(String code, String name, String greeting) {
        this.code = code;
        this.name = name;
        this.greeting = greeting;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGreeting() {
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

    @Override
    public String toString() {
        return this.name;
    }

}
Запуск примера:
ChoiceBox & Separator:
Вы так же можете добавить Separator чтобы отделить элементы ChoiceBox.
ChoiceBoxSeparatorDemo.java
package org.o7planning.javafx.choicebox;

import org.o7planning.javafx.model.Language;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class ChoiceBoxSeparatorDemo extends Application {

    @Override
    public void start(Stage primaryStage) {

        Language vietnamese = new Language("vi", "Vietnamese");
        Language english = new Language("en", "English");
        Language russian = new Language("ru", "Russian");
        Separator separator = new Separator();

        FlowPane root = new FlowPane();

        Label label = new Label("Select Language:");

        ObservableList<?> languages //
                = FXCollections.observableArrayList(vietnamese, separator, english, russian);
       
        ChoiceBox<?> choiceBox = new ChoiceBox<>(languages);

        root.setPadding(new Insets(10));

        root.getChildren().addAll(label, choiceBox);
        root.setPadding(new Insets(10));
        root.setHgap(10);

        primaryStage.setTitle("ChoiceBox (o7planning.org)");
        Scene scene = new Scene(root, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

3. ChoiceBox и событие

// Change Index Listener
choiceBox.getSelectionModel()
           .selectedIndexProperty()
           .addListener(ChangeListener<? super Number>);

// Change Item Listener     
choiceBox.getSelectionModel()
           .selectedItemProperty()
           .addListener(ChangeListener<? super T>);
ChoiceBoxEventDemo.java
package org.o7planning.javafx.choicebox;

import org.o7planning.javafx.model.Language;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class ChoiceBoxEventDemo extends Application {

    @Override
    public void start(Stage primaryStage) {

        Language vietnamese = new Language("vi", "Vietnamese", "Xin Chào");
        Language english = new Language("en", "English", "Hello");
        Language russian = new Language("ru", "Russian", "привет");

        FlowPane root = new FlowPane();

        Label label = new Label("Select Language:");
        Label greetingLabel = new Label("");

        ObservableList<Language> languages //
                = FXCollections.observableArrayList(vietnamese, english, russian);

        ChoiceBox<Language> choiceBox = new ChoiceBox<Language>(languages);

        ChangeListener<Language> changeListener = new ChangeListener<Language>() {

            @Override
            public void changed(ObservableValue<? extends Language> observable, //
                    Language oldValue, Language newValue) {
                if (newValue != null) {
                    greetingLabel.setText(newValue.getGreeting());
                }
            }
        };
        // Selected Item Changed.
        choiceBox.getSelectionModel().selectedItemProperty().addListener(changeListener);

        root.setPadding(new Insets(10));

        root.getChildren().addAll(label, choiceBox, greetingLabel);
        root.setPadding(new Insets(10));
        root.setHgap(10);

        primaryStage.setTitle("ChoiceBox (o7planning.org)");
        Scene scene = new Scene(root, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

Руководства JavaFX

Show More