JavaFX ChoiceBox Tutorial with Examples
2. ChoiceBox Example
No ADS
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;
}
}
Running the example:
ChoiceBox & Separator:
You can also add a Separator to separate the elements of 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 and Event
// 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);
}
}
No ADS
JavaFX Tutorials
- JavaFX TableView Tutorial with Examples
- JavaFX GridPane Layout Tutorial with Examples
- JavaFX Image and ImageView Tutorial with Examples
- JavaFX RadioButton Tutorial with Examples
- Install e(fx)clipse for Eclipse (JavaFX Tooling)
- JavaFX FileChooser and DirectoryChooser Tutorial with Examples
- JavaFX StackPane Layout Tutorial with Examples
- JavaFX ToggleButton Tutorial with Examples
- JavaFX ColorPicker Tutorial with Examples
- JavaFX ContextMenu Tutorial with Examples
- JavaFX TreeView Tutorial with Examples
- JavaFX Hyperlink Tutorial with Examples
- JavaFX DatePicker Tutorial with Examples
- JavaFX Label Tutorial with Examples
- JavaFX Accordion Tutorial with Examples
- JavaFX TilePane Layout Tutorial with Examples
- JavaFX Menu Tutorial with Examples
- JavaFX WebView and WebEngine Tutorial with Examples
- JavaFX ChoiceBox Tutorial with Examples
- JavaFX Effects Tutorial with Examples
- JavaFX TextArea Tutorial with Examples
- JavaFX MenuButton and SplitMenuButton Tutorial with Examples
- JavaFX PieChart Tutorial with Examples
- Install JavaFX Scene Builder for Eclipse
- JavaFX AreaChart and StackedAreaChart Tutorial with Examples
- JavaFX ComboBox Tutorial with Examples
- JavaFX TextInputDialog Tutorial with Examples
- JavaFX Rectangle and Ellipse Tutorial with Examples
- JavaFX HTMLEditor Tutorial with Examples
- JavaFX Button Tutorial with Examples
- JavaFX BarChart and StackedBarChart Tutorial with Examples
- Open a new Window in JavaFX
- JavaFX TitledPane Tutorial with Examples
- JavaFX AnchorPane Layout Tutorial with Examples
- JavaFX Spinner Tutorial with Examples
- JavaFX Tutorial for Beginners - Hello JavaFX
- JavaFX TreeTableView Tutorial with Examples
- JavaFX PasswordField Tutorial with Examples
- JavaFX Slider Tutorial with Examples
- JavaFX ChoiceDialog Tutorial with Examples
- JavaFX TextField Tutorial with Examples
- JavaFX ListView Tutorial with Examples
- JavaFX Alert Dialogs Tutorial with Examples
- JavaFX Line Tutorial with Examples
- JavaFX Tooltip Tutorial with Examples
- JavaFX FlowPane Layout Tutorial with Examples
- JavaFX Transformations Tutorial with Examples
- JavaFX BorderPane Layout Tutorial with Examples
- JavaFX HBox, VBox Layout Tutorial with Examples
- JavaFX Group Tutorial with Examples
- JavaFX ProgressBar and ProgressIndicator Tutorial with Examples
- JavaFX ScrollPane Tutorial with Examples
Show More