Die Anleitung zu JavaFX ChoiceBox
2. Das Beispiel über 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;
}
}
Beispiel
ChoiceBox & Separator:
Sie können Separator addieren, um die Elemente von ChoiceBox. zu trennen
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 und das 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);
}
}
Die Anleitungen JavaFX
- Öffnen Sie ein neues Fenster (window) in JavaFX
- Die Anleitung zu JavaFX ChoiceDialog
- Die Anleitung zu JavaFX Alert Dialog
- Die Anleitung zu JavaFX TextInputDialog
- Installieren Sie e(fx)clipse für Eclipse (JavaFX Tooling)
- Installieren Sie JavaFX Scene Builder für Eclipse
- Die Anleitung zum JavaFX für den Anfänger - Hello JavaFX
- Die Anleitung zu JavaFX FlowPane Layout
- Die Anleitung zu JavaFX TilePane Layout
- Die Anleitung zu JavaFX HBox, VBox Layout
- Die Anleitung zu JavaFX BorderPane Layout
- Die Anleitung zu JavaFX AnchorPane Layout
- Die Anleitung zu JavaFX TitledPane
- Die Anleitung zu JavaFX Accordion
- Die Anleitung zu JavaFX ListView
- Die Anleitung zu JavaFX Group
- Die Anleitung zu JavaFX ComboBox
- Die Anleitung zu JavaFX Transformation
- Effekte (Effects) in JavaFX
- Die Anleitung zu JavaFX GridPane Layout
- Die Anleitung zu JavaFX StackPane Layout
- Die Anleitung zu JavaFX ScrollPane
- Die Anleitung zu JavaFX WebView und WebEngine
- Die Anleitung zu JavaFX HTMLEditor
- Die Anleitung zu JavaFX TableView
- Die Anleitung zu JavaFX TreeView
- Die Anleitung zu JavaFX TreeTableView
- Die Anleitung zu JavaFX Menu
- Die Anleitung zu JavaFX ContextMenu
- Die Anleitung zu JavaFX Image und ImageView
- Die Anleitung zu JavaFX Label
- Die Anleitung zu JavaFX Hyperlink
- Die Anleitung zu JavaFX Button
- Die Anleitung zu JavaFX ToggleButton
- Die Anleitung zu JavaFX RadioButton
- Die Anleitung zu JavaFX MenuButton und SplitMenuButton
- Die Anleitung zu JavaFX TextField
- Die Anleitung zu JavaFX PasswordField
- Die Anleitung zu JavaFX TextArea
- Die Anleitung zu JavaFX Slider
- Die Anleitung zu JavaFX Spinner
- Die Anleitung zu JavaFX ProgressBar und ProgressIndicator
- Die Anleitung zu JavaFX ChoiceBox
- Die Anleitung zu JavaFX Tooltip
- Die Anleitung zu JavaFX DatePicker
- Die Anleitung zu JavaFX ColorPicker
- Die Anleitung zu JavaFX FileChooser und DirectoryChooser
- Die Anleitung zu JavaFX PieChart
- Die Anleitung zu JavaFX AreaChart und StackedAreaChart
- Die Anleitung zu JavaFX BarChart und StackedBarChart
- Die Anleitung zu JavaFX Line
- Die Anleitung zu JavaFX Rectangle und Ellipse
Show More