Table Of Content

JavaFX ComboBox Tutorial

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();

You can also add items to an available ComboBox.
Planet uranus = new Planet("URA", "Uranus");
Planet neptune = new Planet("NEP", "Neptune");

2- ComboBox Example
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 {

    public void start(Stage stage) {

        ComboBox<Planet> comboBox = new ComboBox<Planet>();

        ObservableList<Planet> list = PlanetDAO.getPlanetList();


        FlowPane root = new FlowPane();
        root.setPadding(new Insets(5));
        root.getChildren().add(new Label("Select Planet:"));

        stage.setTitle("ComboxBox (");
        Scene scene = new Scene(root, 350, 300);

    public static void main(String[] args) {

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; = name;

    public String getCode() {
        return code;

    public void setCode(String code) {
        this.code = code;

    public String getName() {
        return name;

    public void setName(String name) { = name;
    public String toString()  {

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;

View more categories: