JavaFX ComboBox Tutorial with Examples
1. JavaFX ComboBox
ComboBox is a component of popular interface. It allows users to select one of options. When users click on ComboBox, a list of options will appear for users to select.
You can create ComboBox from a list of values:
ObservableList<Planet> list = PlanetDAO.getPlanetList();
comboBox.setItems(list);
You can also add items to an available ComboBox.
Planet uranus = new Planet("URA", "Uranus");
Planet neptune = new Planet("NEP", "Neptune");
comboBox.getItems().addAll(uranus,neptune);
2. ComboBox Example
ComboBoxDemo.java
package org.o7planning.javafx.combobox;
import org.o7planning.javafx.model.Planet;
import org.o7planning.javafx.model.PlanetDAO;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class ComboBoxDemo extends Application {
@Override
public void start(Stage stage) {
ComboBox<Planet> comboBox = new ComboBox<Planet>();
ObservableList<Planet> list = PlanetDAO.getPlanetList();
comboBox.setItems(list);
comboBox.getSelectionModel().select(1);
FlowPane root = new FlowPane();
root.setPadding(new Insets(5));
root.setHgap(5);
root.getChildren().add(new Label("Select Planet:"));
root.getChildren().add(comboBox);
stage.setTitle("ComboxBox (o7planning.org)");
Scene scene = new Scene(root, 350, 300);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Planet.java
package org.o7planning.javafx.model;
public class Planet {
public String code;
private String name;
public Planet() {
}
public Planet(String code, String name) {
this.code = code;
this.name = name;
}
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;
}
@Override
public String toString() {
return this.name;
}
}
PlanetDAO.java
package org.o7planning.javafx.model;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class PlanetDAO {
public static ObservableList<Planet> getPlanetList() {
Planet mercury = new Planet("MER", "Mercury");
Planet venus = new Planet("VEN", "Venus");
Planet earth = new Planet("EAR", "Earth");
ObservableList<Planet> list //
= FXCollections.observableArrayList(mercury, venus, earth);
return list;
}
}
JavaFX Tutorials
- Open a new Window in JavaFX
- JavaFX ChoiceDialog Tutorial with Examples
- JavaFX Alert Dialogs Tutorial with Examples
- JavaFX TextInputDialog Tutorial with Examples
- Install e(fx)clipse for Eclipse (JavaFX Tooling)
- Install JavaFX Scene Builder for Eclipse
- JavaFX Tutorial for Beginners - Hello JavaFX
- JavaFX FlowPane Layout Tutorial with Examples
- JavaFX TilePane Layout Tutorial with Examples
- JavaFX HBox, VBox Layout Tutorial with Examples
- JavaFX BorderPane Layout Tutorial with Examples
- JavaFX AnchorPane Layout Tutorial with Examples
- JavaFX TitledPane Tutorial with Examples
- JavaFX Accordion Tutorial with Examples
- JavaFX ListView Tutorial with Examples
- JavaFX Group Tutorial with Examples
- JavaFX ComboBox Tutorial with Examples
- JavaFX Transformations Tutorial with Examples
- JavaFX Effects Tutorial with Examples
- JavaFX GridPane Layout Tutorial with Examples
- JavaFX StackPane Layout Tutorial with Examples
- JavaFX ScrollPane Tutorial with Examples
- JavaFX WebView and WebEngine Tutorial with Examples
- JavaFX HTMLEditor Tutorial with Examples
- JavaFX TableView Tutorial with Examples
- JavaFX TreeView Tutorial with Examples
- JavaFX TreeTableView Tutorial with Examples
- JavaFX Menu Tutorial with Examples
- JavaFX ContextMenu Tutorial with Examples
- JavaFX Image and ImageView Tutorial with Examples
- JavaFX Label Tutorial with Examples
- JavaFX Hyperlink Tutorial with Examples
- JavaFX Button Tutorial with Examples
- JavaFX ToggleButton Tutorial with Examples
- JavaFX RadioButton Tutorial with Examples
- JavaFX MenuButton and SplitMenuButton Tutorial with Examples
- JavaFX TextField Tutorial with Examples
- JavaFX PasswordField Tutorial with Examples
- JavaFX TextArea Tutorial with Examples
- JavaFX Slider Tutorial with Examples
- JavaFX Spinner Tutorial with Examples
- JavaFX ProgressBar and ProgressIndicator Tutorial with Examples
- JavaFX ChoiceBox Tutorial with Examples
- JavaFX Tooltip Tutorial with Examples
- JavaFX DatePicker Tutorial with Examples
- JavaFX ColorPicker Tutorial with Examples
- JavaFX FileChooser and DirectoryChooser Tutorial with Examples
- JavaFX PieChart Tutorial with Examples
- JavaFX AreaChart and StackedAreaChart Tutorial with Examples
- JavaFX BarChart and StackedBarChart Tutorial with Examples
- JavaFX Line Tutorial with Examples
- JavaFX Rectangle and Ellipse Tutorial with Examples
Show More