Руководство 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
- Откройте новое окно (window) в JavaFX
- Руководство JavaFX ChoiceDialog
- Руководство JavaFX Alert Dialog
- Руководство JavaFX TextInputDialog
- Установите e(fx)clipse для Eclipse (JavaFX Tooling)
- Установите JavaFX Scene Builder для Eclipse
- Руководство JavaFX для начинающих - Hello JavaFX
- Руководство JavaFX FlowPane Layout
- Руководство JavaFX TilePane Layout
- Руководство JavaFX HBox, VBox Layout
- Руководство JavaFX BorderPane Layout
- Руководство JavaFX AnchorPane Layout
- Руководство JavaFX TitledPane
- Руководство JavaFX Accordion
- Руководство JavaFX ListView
- Руководство JavaFX Group
- Руководство JavaFX ComboBox
- Руководство JavaFX Transformations
- Эффекты (effects) в JavaFX
- Руководство JavaFX GridPane Layout
- Руководство JavaFX StackPane Layout
- Руководство JavaFX ScrollPane
- Руководство JavaFX WebView и WebEngine
- Руководство JavaFX HTMLEditor
- Руководство JavaFX TableView
- Руководство JavaFX TreeView
- Руководство JavaFX TreeTableView
- Руководство JavaFX Menu
- Руководство JavaFX ContextMenu
- Руководство JavaFX Image и ImageView
- Руководство JavaFX Label
- Руководство JavaFX Hyperlink
- Руководство JavaFX Button
- Руководство JavaFX ToggleButton
- Руководство JavaFX RadioButton
- Руководство JavaFX MenuButton и SplitMenuButton
- Руководство JavaFX TextField
- Руководство JavaFX PasswordField
- Руководство JavaFX TextArea
- Руководство JavaFX Slider
- Руководство JavaFX Spinner
- Руководство JavaFX ProgressBar и ProgressIndicator
- Руководство JavaFX ChoiceBox
- Руководство JavaFX Tooltip
- Руководство JavaFX DatePicker
- Руководство JavaFX ColorPicker
- Руководство JavaFX FileChooser и DirectoryChooser
- Руководство JavaFX PieChart
- Руководство JavaFX AreaChart и StackedAreaChart
- Руководство JavaFX BarChart и StackedBarChart
- Руководство JavaFX Line
- Руководство JavaFX Rectangle и Ellipse
Show More