Le Tutoriel de JavaFX ChoiceBox
2. Exemple de 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;
}
}
Exécutez l’exemple :
ChoiceBox & Separator:
Vous pouvez également ajouter Separator pour séparer les éléments de 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 et événement
// 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);
}
}
Tutoriels de JavaFX
- Ouvrir une nouvelle fenêtre (Window) dans JavaFX
- Le Tutoriel de JavaFX ChoiceDialog
- Le Tutoriel de JavaFX Alert Dialog
- Le Tutoriel de JavaFX TextInputDialog
- Installer e(fx)clipse pour Eclipse (Outillage JavaFX)
- Installer JavaFX Scene Builder pour Eclipse
- Tutoriel JavaFX pour débutant - Hello JavaFX
- Le Tutoriel de JavaFX FlowPane Layout
- Le Tutoriel de JavaFX TilePane Layout
- Le Tutoriel de JavaFX HBox et VBox Layout
- Le Tutoriel de JavaFX BorderPane Layout
- Le Tutoriel de JavaFX AnchorPane Layout
- Le Tutoriel de JavaFX TitledPane
- Le Tutoriel de JavaFX Accordion
- Le Tutoriel de JavaFX ListView
- Le Tutoriel de JavaFX Group
- Le Tutoriel de JavaFX ComboBox
- Transformations dans JavaFX
- Les effets (effects) dans JavaFX
- Le Tutoriel de JavaFX GridPane Layout
- Le Tutoriel de JavaFX StackPane Layout
- Le Tutoriel de JavaFX ScrollPane
- Le Tutoriel de JavaFX WebView et WebEngine
- Le Tutoriel de JavaFX HTMLEditor
- Le Tutoriel de JavaFX TableView
- Le Tutoriel de JavaFX TreeView
- Le Tutoriel de JavaFX TreeTableView
- Le Tutoriel de JavaFX Menu
- Le Tutoriel de JavaFX ContextMenu
- Le Tutoriel de JavaFX Image et ImageView
- Le Tutoriel de JavaFX Label
- Le Tutoriel de JavaFX Hyperlink
- Le Tutoriel de JavaFX Button
- Le Tutoriel de JavaFX ToggleButton
- Le Tutoriel de JavaFX RadioButton
- Le Tutoriel de JavaFX MenuButton et SplitMenuButton
- Le Tutoriel de JavaFX TextField
- Le Tutoriel de JavaFX PasswordField
- Le Tutoriel de JavaFX TextArea
- Le Tutoriel de JavaFX Slider
- Le Tutoriel de JavaFX Spinner
- Le Tutoriel de JavaFX ProgressBar et ProgressIndicator
- Le Tutoriel de JavaFX ChoiceBox
- Le Tutoriel de JavaFX Tooltip
- Le Tutoriel de JavaFX DatePicker
- Le Tutoriel de JavaFX ColorPicker
- Le Tutoriel de JavaFX FileChooser et DirectoryChooser
- Le Tutoriel de JavaFX PieChart
- Le Tutoriel de JavaFX AreaChart et StackedAreaChart
- Le Tutoriel de JavaFX BarChart et StackedBarChart
- Le Tutoriel de JavaFX Line
- Le Tutoriel de JavaFX Rectangle et Ellipse
Show More