A simple JavaFX tutorial through simple snippets.

Example 1: Combobox

Here are the code:

Step 1: Create Project

  1. Open your favorite Java IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following java files:

  • ComboBoxExample.java
  1. In your editor or IDE, create a file known as ComboBoxExample.java.
  2. Then add the following code:

(a). ComboBoxExample.java

First, go ahead and add the following imports:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

We will need to extend our class using the extend keyword. By doing that our class can make use of inheritance to derive properties and functions defined in the parent class.

public class ComboBoxExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

Every Java Program must have a main method. This is the entry point of all Java applications include JavaFX. Add a main() method and inside it invoke the launch() function.

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {

        ComboBox comboBox = new ComboBox();

        comboBox.getItems().add("Choice 0");
        comboBox.getItems().add("Choice 1");
        comboBox.getItems().add("Choice 2");

        comboBox.setEditable(true);

        comboBox.setOnAction((event) -> {
            int selectedIndex = comboBox.getSelectionModel().getSelectedIndex();
            Object selectedItem = comboBox.getSelectionModel().getSelectedItem();

            System.out.println("Selection made: [" + selectedIndex + "] " + selectedItem);
            System.out.println("   ChoiceBox.getValue(): " + comboBox.getValue());
        });

        VBox vbox = new VBox(comboBox);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.setTitle("ComboBox Example");
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.combobox;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * Shows a simple JavaFX Button - and prints a text when the button is clicked.
 */
public class ComboBoxExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        ComboBox comboBox = new ComboBox();

        comboBox.getItems().add("Choice 0");
        comboBox.getItems().add("Choice 1");
        comboBox.getItems().add("Choice 2");

        comboBox.setEditable(true);

        comboBox.setOnAction((event) -> {
            int selectedIndex = comboBox.getSelectionModel().getSelectedIndex();
            Object selectedItem = comboBox.getSelectionModel().getSelectedItem();

            System.out.println("Selection made: [" + selectedIndex + "] " + selectedItem);
            System.out.println("   ChoiceBox.getValue(): " + comboBox.getValue());
        });

        VBox vbox = new VBox(comboBox);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.setTitle("ComboBox Example");
        primaryStage.show();
    }

}

Download

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License