These are simple step by step code snippets to allow you learn JavaFX.

Example 1: Tableview

Study the following 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:

  • Customer.java
  • Person.java
  • TableViewCustomRenderingExample.java
  • TableViewEditableExample.java
  • TableViewExample.java
  • TableViewMapDataItemsExample.java
  • TableViewNestedColumnsExample.java
  • TableViewSelectionModelExample.java
  1. In your editor or IDE, create a file known as Customer.java.
  2. Then add the following code:

(a). Customer.java

So please create a simple class as shown below:

public class Customer {

Our class will have the following methods:

  • Customer(String customerNo, String firstName, String lastName)
  • String getCustomerNo()
  • void setCustomerNo(String customerNo)
  • String getFirstName()
  • void setFirstName(String firstName)
  • String getLastName()
  • void setLastName(String lastName)

Here is the full code:

package com.jenkov.javafx.tableview;

public class Customer {
    private String customerNo = null;
    private String firstName = null;
    private String lastName = null;

    public Customer(String customerNo, String firstName, String lastName){
        this.customerNo = customerNo;
        this.firstName  = firstName;
        this.lastName   = lastName;
    }

    public String getCustomerNo() {
        return customerNo;
    }

    public void setCustomerNo(String customerNo) {
        this.customerNo = customerNo;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}
  1. Next create another file known as Person.java.
  2. And add the following code:

(b). Person.java

So please create a simple class as shown below:

public class Person {

Our class will have the following methods:

  • Person(String firstName, String lastName)
  • Person(String firstName, String lastName, String category, boolean isXyz)
  • String getFirstName()
  • void setFirstName(String firstName)
  • String getLastName()
  • void setLastName(String lastName)
  • String toString()
  • String getCategory()
  • void setCategory(String category)
  • Boolean getIsXyz()
  • void setIsXyz(Boolean isXyz)

Here is the full code:

package com.jenkov.javafx.tableview;

public class Person {

    private String firstName = null;
    private String lastName = null;

    private String category = null;

    private boolean isXyz = true;

    public Person() {
    }

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Person(String firstName, String lastName, String category, boolean isXyz) {
        this.firstName = firstName;
        this.lastName  = lastName;
        this.category  = category;
        this.isXyz     = isXyz;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String toString() {
        return "{ firstName: '" + this.firstName + ", lastName: " + this.lastName + " }";
    }

    public String getCategory() {
        return this.category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Boolean getIsXyz() {
        return this.isXyz;
    }

    public void setIsXyz(Boolean isXyz) {
        this.isXyz = isXyz;
    }
}
  1. Next create another file known as TableViewCustomRenderingExample.java.
  2. And add the following code:

(c). TableViewCustomRenderingExample.java

After creating our class, the first thing is to define imports. Such imports are ready made classes that inject more functionalities into our project.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class TableViewCustomRenderingExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)
  • protected void updateItem(String item, boolean empty)

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) {

We have 2methods to override in this class. Here are those methods:

  • public void start(Stage primaryStage) {
  • protected void updateItem(String item, boolean empty) {
    Prepend the @Override modifier to your method. Then add implementation code as follows:
    @Override
    public void start(Stage primaryStage)  {

        TableView tableView = new TableView();

        TableColumn<Customer, String> customerColumn = new TableColumn<>("Customer");

        customerColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
        customerColumn.setCellFactory((tableColumn) -> {
            TableCell<Customer, String> tableCell = new TableCell<>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);

                    this.setText(null);
                    this.setGraphic(null);

                    if(!empty){
                        this.setText(item.toUpperCase());
                    }

Here is the full code:

package com.jenkov.javafx.tableview;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableViewCustomRenderingExample extends Application {

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

    @Override
    public void start(Stage primaryStage)  {

        TableView tableView = new TableView();

        TableColumn<Customer, String> customerColumn = new TableColumn<>("Customer");

        customerColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
        customerColumn.setCellFactory((tableColumn) -> {
            TableCell<Customer, String> tableCell = new TableCell<>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);

                    this.setText(null);
                    this.setGraphic(null);

                    if(!empty){
                        this.setText(item.toUpperCase());
                    }
                }
            };

            return tableCell;
        });

        tableView.getColumns().add(customerColumn);

        tableView.getItems().add(new Customer("007", "Jane", "Deer"));
        tableView.getItems().add(new Customer("006", "John", "Doe"));
        tableView.getItems().add(new Customer("008", "Mack", "Alamo"));

        VBox vbox = new VBox(tableView);
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX TableView Example");

        primaryStage.show();
    }
}
  1. Next create another file known as TableViewEditableExample.java.
  2. And add the following code:

(d). TableViewEditableExample.java

First, go ahead and add the following imports:

import javafx.application.Application;
import javafx.beans.property.ReadOnlyBooleanWrapper;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

First, go ahead and add the following imports:

import java.io.FileNotFoundException;

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 TableViewEditableExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage) throws InterruptedException, FileNotFoundException

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) throws InterruptedException, FileNotFoundException method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) throws InterruptedException, FileNotFoundException {

        TableView tableView = new TableView();
        tableView.setEditable(true);

        TableColumn<Person, String> column1 = new TableColumn<>("First Name");
        column1.setCellValueFactory(new PropertyValueFactory<>("firstName"));

        TableColumn<Person, String> column2 = new TableColumn<>("Last Name");
        column2.setCellValueFactory(new PropertyValueFactory<>("lastName"));
        column2.setCellFactory(TextFieldTableCell.<Person>forTableColumn());

        TableColumn<Person, String> column3 = new TableColumn<>("Category");
        column3.setCellValueFactory(new PropertyValueFactory<>("category"));
        column3.setCellFactory(ComboBoxTableCell.<Person, String>forTableColumn("Category 1", "Category 2"));

        TableColumn<Person, Boolean> column4 = new TableColumn<>("Is XYZ");
        column4.setCellValueFactory( cellData -> new ReadOnlyBooleanWrapper(cellData.getValue().getIsXyz()));
        column4.setCellFactory(CheckBoxTableCell.<Person>forTableColumn(column4));

        tableView.getColumns().add(column1);
        tableView.getColumns().add(column2);
        tableView.getColumns().add(column3);
        tableView.getColumns().add(column4);

        tableView.getItems().add(new Person("John"  , "Doe"  , null, false));
        tableView.getItems().add(new Person("Jane"  , "Deer" , "Category 1", true));
        tableView.getItems().add(new Person("Dinesh", "Gupta", "Category 2", true));

        VBox vbox = new VBox(tableView);
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX Editable TableView Example");

        primaryStage.show();

        System.out.println("Opened");
    }

Here is the full code:

package com.jenkov.javafx.tableview;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyBooleanWrapper;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.FileNotFoundException;

public class TableViewEditableExample extends Application {

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

    @Override
    public void start(Stage primaryStage) throws InterruptedException, FileNotFoundException {

        TableView tableView = new TableView();
        tableView.setEditable(true);

        TableColumn<Person, String> column1 = new TableColumn<>("First Name");
        column1.setCellValueFactory(new PropertyValueFactory<>("firstName"));

        TableColumn<Person, String> column2 = new TableColumn<>("Last Name");
        column2.setCellValueFactory(new PropertyValueFactory<>("lastName"));
        column2.setCellFactory(TextFieldTableCell.<Person>forTableColumn());

        TableColumn<Person, String> column3 = new TableColumn<>("Category");
        column3.setCellValueFactory(new PropertyValueFactory<>("category"));
        column3.setCellFactory(ComboBoxTableCell.<Person, String>forTableColumn("Category 1", "Category 2"));

        TableColumn<Person, Boolean> column4 = new TableColumn<>("Is XYZ");
        column4.setCellValueFactory( cellData -> new ReadOnlyBooleanWrapper(cellData.getValue().getIsXyz()));
        column4.setCellFactory(CheckBoxTableCell.<Person>forTableColumn(column4));

        tableView.getColumns().add(column1);
        tableView.getColumns().add(column2);
        tableView.getColumns().add(column3);
        tableView.getColumns().add(column4);

        tableView.getItems().add(new Person("John"  , "Doe"  , null, false));
        tableView.getItems().add(new Person("Jane"  , "Deer" , "Category 1", true));
        tableView.getItems().add(new Person("Dinesh", "Gupta", "Category 2", true));

        VBox vbox = new VBox(tableView);
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX Editable TableView Example");

        primaryStage.show();

        System.out.println("Opened");
    }
}
  1. Next create another file known as TableViewExample.java.
  2. And add the following code:

(e). TableViewExample.java

First, go ahead and add the following imports:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

First, go ahead and add the following imports:

import java.io.FileNotFoundException;

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 TableViewExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage) throws InterruptedException, FileNotFoundException

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) throws InterruptedException, FileNotFoundException method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) throws InterruptedException, FileNotFoundException {

        TableView tableView = new TableView();

        TableColumn<Person, String> column1 = new TableColumn<>("First Name");
        column1.setCellValueFactory(new PropertyValueFactory<>("firstName"));

        TableColumn<Person, String> column2 = new TableColumn<>("Last Name");
        column2.setCellValueFactory(new PropertyValueFactory<>("lastName"));

        tableView.getColumns().add(column1);
        tableView.getColumns().add(column2);

        tableView.getItems().add(new Person("John", "Doe"));
        tableView.getItems().add(new Person("Jane", "Deer"));

        VBox vbox = new VBox(tableView);
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX TableView Example");

        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.tableview;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.FileNotFoundException;

public class TableViewExample extends Application {

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

    @Override
    public void start(Stage primaryStage) throws InterruptedException, FileNotFoundException {

        TableView tableView = new TableView();

        TableColumn<Person, String> column1 = new TableColumn<>("First Name");
        column1.setCellValueFactory(new PropertyValueFactory<>("firstName"));

        TableColumn<Person, String> column2 = new TableColumn<>("Last Name");
        column2.setCellValueFactory(new PropertyValueFactory<>("lastName"));

        tableView.getColumns().add(column1);
        tableView.getColumns().add(column2);

        tableView.getItems().add(new Person("John", "Doe"));
        tableView.getItems().add(new Person("Jane", "Deer"));

        VBox vbox = new VBox(tableView);
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX TableView Example");

        primaryStage.show();
    }
}
  1. Next create another file known as TableViewMapDataItemsExample.java.
  2. And add the following code:

(f). TableViewMapDataItemsExample.java

First, go ahead and add the following imports:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

First, go ahead and add the following imports:

import java.util.HashMap;
import java.util.Map;

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 TableViewMapDataItemsExample 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) {

        TableView tableView = new TableView();

        ObservableList<Map<String, Object>> items = FXCollections.<Map<String, Object>>observableArrayList();

        Map<String, Object> item1 = new HashMap<>();
        item1.put("firstName", "Randall");
        item1.put("lastName" , "Kovic");

        items.add(item1);

        Map<String, Object> item2 = new HashMap<>();
        item2.put("firstName", "Irmelin");
        item2.put("lastName" , "Satoshi");

        items.add(item2);

        tableView.getItems().addAll(items);

        TableColumn<Map, String> firstNameColumn = new TableColumn<>("firstName");
        firstNameColumn.setCellValueFactory(new MapValueFactory<>("firstName"));

        TableColumn<Map, String> lastNameColumn = new TableColumn<>("lastName");
        lastNameColumn.setCellValueFactory(new MapValueFactory<>("lastName"));

        tableView.getColumns().add(firstNameColumn);
        tableView.getColumns().add(lastNameColumn);

        VBox vbox = new VBox(tableView);
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX TableView Example");

        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.tableview;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Map;

public class TableViewMapDataItemsExample extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        TableView tableView = new TableView();

        ObservableList<Map<String, Object>> items = FXCollections.<Map<String, Object>>observableArrayList();

        Map<String, Object> item1 = new HashMap<>();
        item1.put("firstName", "Randall");
        item1.put("lastName" , "Kovic");

        items.add(item1);

        Map<String, Object> item2 = new HashMap<>();
        item2.put("firstName", "Irmelin");
        item2.put("lastName" , "Satoshi");

        items.add(item2);

        tableView.getItems().addAll(items);

        TableColumn<Map, String> firstNameColumn = new TableColumn<>("firstName");
        firstNameColumn.setCellValueFactory(new MapValueFactory<>("firstName"));

        TableColumn<Map, String> lastNameColumn = new TableColumn<>("lastName");
        lastNameColumn.setCellValueFactory(new MapValueFactory<>("lastName"));

        tableView.getColumns().add(firstNameColumn);
        tableView.getColumns().add(lastNameColumn);

        VBox vbox = new VBox(tableView);
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX TableView Example");

        primaryStage.show();
    }
}
  1. Next create another file known as TableViewNestedColumnsExample.java.
  2. And add the following code:

(g). TableViewNestedColumnsExample.java

First, go ahead and add the following imports:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
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 TableViewNestedColumnsExample 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)  {

        TableView tableView = new TableView();

        TableColumn<Customer, String> customer = new TableColumn<>("Customer");

        TableColumn<Customer, String> customerId = new TableColumn<>("Customer No");

        TableColumn<Customer, String> name       = new TableColumn<>("Name");
        TableColumn<Customer, String> firstName  = new TableColumn<>("First Name");
        TableColumn<Customer, String> lastName   = new TableColumn<>("Last Name");

        name.getColumns().addAll(firstName, lastName);
        customer.getColumns().addAll(customerId, name);
        tableView.getColumns().addAll(customer);

        customerId.setCellValueFactory(new PropertyValueFactory<>("customerNo"));
        firstName .setCellValueFactory(new PropertyValueFactory<>("firstName"));
        lastName  .setCellValueFactory(new PropertyValueFactory<>("lastName"));

        tableView.getItems().add(new Customer("007", "Jane", "Deer"));
        tableView.getItems().add(new Customer("006", "John", "Doe"));
        tableView.getItems().add(new Customer("008", "Mack", "Alamo"));

        TableColumn<String, Customer> visibleLeafColumn = tableView.getVisibleLeafColumn(1);

        VBox vbox = new VBox(tableView);
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX TableView Nested Columns Example");

        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.tableview;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableViewNestedColumnsExample extends Application {

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

    @Override
    public void start(Stage primaryStage)  {

        TableView tableView = new TableView();

        TableColumn<Customer, String> customer = new TableColumn<>("Customer");

        TableColumn<Customer, String> customerId = new TableColumn<>("Customer No");

        TableColumn<Customer, String> name       = new TableColumn<>("Name");
        TableColumn<Customer, String> firstName  = new TableColumn<>("First Name");
        TableColumn<Customer, String> lastName   = new TableColumn<>("Last Name");

        name.getColumns().addAll(firstName, lastName);
        customer.getColumns().addAll(customerId, name);
        tableView.getColumns().addAll(customer);

        customerId.setCellValueFactory(new PropertyValueFactory<>("customerNo"));
        firstName .setCellValueFactory(new PropertyValueFactory<>("firstName"));
        lastName  .setCellValueFactory(new PropertyValueFactory<>("lastName"));

        tableView.getItems().add(new Customer("007", "Jane", "Deer"));
        tableView.getItems().add(new Customer("006", "John", "Doe"));
        tableView.getItems().add(new Customer("008", "Mack", "Alamo"));

        TableColumn<String, Customer> visibleLeafColumn = tableView.getVisibleLeafColumn(1);

        VBox vbox = new VBox(tableView);
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX TableView Nested Columns Example");

        primaryStage.show();
    }

}
  1. Next create another file known as TableViewSelectionModelExample.java.
  2. And add the following code:

(h). TableViewSelectionModelExample.java

First, go ahead and add the following imports:

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

First, go ahead and add the following imports:

import java.io.FileNotFoundException;

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 TableViewSelectionModelExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage) throws InterruptedException, FileNotFoundException
  • void onChanged(Change<? extends Person> change)

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) {

We have 2methods to override in this class. Here are those methods:

  • public void start(Stage primaryStage) throws InterruptedException, FileNotFoundException {
  • public void onChanged(Change<? extends Person> change) {
    Prepend the @Override modifier to your method. Then add implementation code as follows:
    @Override
    public void start(Stage primaryStage) throws InterruptedException, FileNotFoundException {

        TableView tableView = new TableView();

        TableColumn<Person, String> column1 = new TableColumn<>("First Name");
        column1.setCellValueFactory(new PropertyValueFactory<>("firstName"));

        TableColumn<Person, String> column2 = new TableColumn<>("Last Name");
        column2.setCellValueFactory(new PropertyValueFactory<>("lastName"));

        tableView.getColumns().add(column1);
        tableView.getColumns().add(column2);

        tableView.getItems().add(new Person("John", "Doe"));
        tableView.getItems().add(new Person("Jane", "Deer"));

        TableView.TableViewSelectionModel<Person> selectionModel = tableView.getSelectionModel();

        selectionModel.setSelectionMode(SelectionMode.SINGLE);
        selectionModel.setSelectionMode(SelectionMode.MULTIPLE);

        ObservableList<Person> selectedItems = selectionModel.getSelectedItems();

        selectedItems.addListener(new ListChangeListener<Person>() {
            @Override
            public void onChanged(Change<? extends Person> change) {
                System.out.println("Selection changed: " + change.getList());
            }

Here is the full code:

package com.jenkov.javafx.tableview;

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.FileNotFoundException;

public class TableViewSelectionModelExample extends Application {

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

    @Override
    public void start(Stage primaryStage) throws InterruptedException, FileNotFoundException {

        TableView tableView = new TableView();

        TableColumn<Person, String> column1 = new TableColumn<>("First Name");
        column1.setCellValueFactory(new PropertyValueFactory<>("firstName"));

        TableColumn<Person, String> column2 = new TableColumn<>("Last Name");
        column2.setCellValueFactory(new PropertyValueFactory<>("lastName"));

        tableView.getColumns().add(column1);
        tableView.getColumns().add(column2);

        tableView.getItems().add(new Person("John", "Doe"));
        tableView.getItems().add(new Person("Jane", "Deer"));

        TableView.TableViewSelectionModel<Person> selectionModel = tableView.getSelectionModel();

        selectionModel.setSelectionMode(SelectionMode.SINGLE);
        selectionModel.setSelectionMode(SelectionMode.MULTIPLE);

        ObservableList<Person> selectedItems = selectionModel.getSelectedItems();

        selectedItems.addListener(new ListChangeListener<Person>() {
            @Override
            public void onChanged(Change<? extends Person> change) {
                System.out.println("Selection changed: " + change.getList());
            }
        });

        selectionModel.select(1);

        ObservableList<Integer> selectedIndices = selectionModel.getSelectedIndices();

        VBox vbox = new VBox(tableView);
        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX TableView 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