JavaFX: Mastering FXML

3 Creating an Address Book with FXML

In this chapter, you create an Address Book application that includes a table of names and email addresses, as shown in Figure 3-1. The tutorial shows how to populate a table with data, sort the data at application startup, align the data in the table cells, and add rows to the table.

Some amount of knowledge of FXML and application development is assumed for this tutorial. Before you start, you should have completed the FXML tutorial in the Getting Started series, because it teaches the basics of FXML development. Specifically, for the Address Book tutorial, you should know:

  • The basic structure of an FXML project (.java, .fxml, and controller files)

  • How to create and run a JavaFX FXML project in NetBeans IDE

  • The basics of layout and user interface components

Before you begin this tutorial, ensure that the version of NetBeans IDE that you are using supports your version of JavaFX.

Figure 3-1 Address Book Application

Description of Figure 3-1 follows
Description of "Figure 3-1 Address Book Application"

Set Up the Project

Your first task is to set up a JavaFX FXML project in NetBeans IDE.

  1. From the File menu, choose New Project.

  2. In the JavaFX category, choose JavaFX FXML Application. Click Next.

  3. Name the project FXMLTableView and click Finish.

    NetBeans IDE opens an FXML project that includes the code for a sample Hello World application. The application includes three files:, FXMLDocument.fxml, and

  4. Rename to so that the name is more meaningful for this application.

    1. In the Projects window, right-click and choose Refactor then Rename.

    2. Enter FXMLTableViewController, and then click Refactor.

  5. Rename FXMLDocument.fxml to fxml_tableview.fxml.

    1. Right-click FXMLDocument.fxml and choose Rename.

    2. Enter fxml_tableview and click OK.

  6. Open and edit the FXMLTableView class to look like Example 3-1.

    Example 3-1

    public class FXMLTableView extends Application {
        public void start(Stage primaryStage) throws Exception {
           primaryStage.setTitle("FXML TableView Example");
           Pane myPane = (Pane)FXMLLoader.load(getClass().getResource
           Scene myScene = new Scene(myPane);
        public static void main(String[] args) {

    Note that the Java file does not contain the code for the scene. In the next section of the tutorial, Create the Basic User Interface, you will add the code for the scene in the FXML file.

  7. Press Ctrl (or Cmd) + Shift + I to correct the import statements.

Create the Basic User Interface

Define the user interface by creating a GridPane layout container as the root node of the scene. Then, add a Label and a TableView component as child nodes of the GridPane layout container.

  1. Edit the fxml_tableview.fxml file.

  2. Delete the <AnchorPane> markup that NetBeans IDE automatically generated.

  3. Add a GridPane layout container as the root node of the scene as shown in Example 3-2.

    Example 3-2 GridPane

    <GridPane alignment="CENTER" hgap="10.0" vgap="10.0"
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />

    You can ignore the error "File not found in the specified address:" that might appear in the output window.

  4. Add a Label and a TableView component to the GridPane layout container. The code is in Example 3-3.

    Example 3-3 Label and TableView

    <GridPane alignment="CENTER" hgap="10.0" vgap="10.0"
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
        <Label style="-fx-font: NORMAL 20 Tahoma;" text="Address Book"                
            GridPane.columnIndex="0" GridPane.rowIndex="0">
        <TableView fx:id="tableView" GridPane.columnIndex="0" 
  5. Add the import statement for the Insets class.

    <?import javafx.geometry.Insets?>

  6. Run the program. You will see the label Address Book and a table with the text "No columns in table," which is the default caption defined by the TableView implementation, as shown in Figure 3-2.

    Figure 3-2 Table with No Columns

    Description of Figure 3-2 follows
    Description of "Figure 3-2 Table with No Columns"

Add Columns to the Table

Use the TableColumn class to add three columns for displaying the data: First Name, Last Name, and Email Address. The code is in Example 3-4.

Example 3-4 Table Columns

<TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
          <TableColumn text="First Name">
          <TableColumn text="Last Name">
          <TableColumn text="Email Address">

Tip: For more information on the TableColumn class or any other JavaFX class discussed in this tutorial, see the API documentation.

Figure 3-3 shows the table with the columns for First Name, Last Name, and Email Address.

Figure 3-3 Address Book with Three Columns

Description of Figure 3-3 follows
Description of "Figure 3-3 Address Book with Three Columns"

Define the Data Model

When you create a table in a JavaFX application, it is a best practice to implement a class that defines the data model and provides methods and fields to further work with the table. Create a Person class to define the data for the address book.

  1. In NetBeans IDE, right-click the fxmltableview folder under Source Packages, and choose New then Java Class.

  2. Name the class Person and then click Finish.

  3. Implement a Person class to define the data, as shown in Example 3-5.

    Example 3-5 Person Class

    package fxmltableview;
    public class Person {
       private final SimpleStringProperty firstName = new SimpleStringProperty("");
       private final SimpleStringProperty lastName = new SimpleStringProperty("");
       private final SimpleStringProperty email = new SimpleStringProperty("");
    public Person() {
            this("", "", "");
        public Person(String firstName, String lastName, String email) {
        public String getFirstName() {
            return firstName.get();
        public void setFirstName(String fName) {
        public String getLastName() {
            return lastName.get();
        public void setLastName(String fName) {
        public String getEmail() {
            return email.get();
        public void setEmail(String fName) {

Associate Data with the Table Columns

The next tasks are to define rows for the data and associate the data with the table columns. You add this code to the FXML file.

  1. In the fxml_tableview.fxml file, create an ObservableList array and define as many data rows as you would like to show in your table. Sample code is in Example 3-6. Add the code between the </columns> and </TableView> markup.

    Example 3-6 ObservableList Array

        <FXCollections fx:factory="observableArrayList">
            <Person firstName="Jacob" lastName="Smith"  
            <Person firstName="Isabella" lastName="Johnson" 
            <Person firstName="Ethan" lastName="Williams" 
            <Person firstName="Emma" lastName="Jones"
            <Person firstName="Michael" lastName="Brown" 
  2. Specify a cell factory for each column to associate the data with the column, as shown in Example 3-7.

    Example 3-7 Cell Factories

         <TableColumn text="First Name">
            <cellValueFactory><PropertyValueFactory property="firstName" />
         <TableColumn text="Last Name">
            <cellValueFactory><PropertyValueFactory property="lastName" />
         <TableColumn text="Email Address">
             <cellValueFactory><PropertyValueFactory property="email" />

    Cell factories are implemented by using the PropertyValueFactory class, which uses the firstName, lastName, and email properties of the table columns as references to the corresponding methods of the Person class.

  3. Import the required packages, as shown in Example 3-8:

    Example 3-8 Import Statements

    <?import javafx.scene.control.cell.*?> 
    <?import javafx.collections.*?> 
    <?import fxmltableview.*?> 

Running the application at this point shows the table populated with data, as shown in Figure 3-4.

Figure 3-4 Table with Data

Description of Figure 3-4 follows
Description of "Figure 3-4 Table with Data"

Here are some built-in features of the TableView class for you to try:

  • Resize a column width by dragging the column divider in the table header to the left or right.

  • Move a column by dragging the column header.

  • Alter the sort order of data by clicking a column header. The first click enables an ascending sort order, the second click enables a descending sort order, and the third click disables sorting. By default, no sorting is applied.

Set Sort Order on Startup

In this task, you set the sort order so that the entries in the First Name column appear in ascending alphabetical order on application startup. You do this by creating an ID for the table column and then setting up a reference to it.

  1. Add an ID to the First Name column:

    <TableColumn fx:id="firstNameColumn" text="First Name">

  2. Specify the sort order by adding the code in Example 3-9 between the </items> and </TableView> markup.

    Example 3-9 Sort Order

              <fx:reference source="firstNameColumn"/>

You can see the results in Figure 3-5.

Figure 3-5 Table with First Column Data Sorted at Startup

Description of Figure 3-5 follows
Description of "Figure 3-5 Table with First Column Data Sorted at Startup"

Define Column Widths

Add the prefWidth property to increase the column widths, as shown in Example 3-10.

Example 3-10 Column Widths

<TableColumn fx:id="firstnameColumn" text="First Name" prefWidth="100">
        <cellValueFactory><PropertyValueFactory property="firstName" />
     <TableColumn text="Last Name" prefWidth="100">
        <cellValueFactory><PropertyValueFactory property="lastName" />
     <TableColumn text="Email Address" prefWidth="200">
        <cellValueFactory><PropertyValueFactory property="email" />

The result is in Figure 3-6. The column widths have been increased so that all data is visible in each table row.

Figure 3-6 Table with Column Widths Set

Description of Figure 3-6 follows
Description of "Figure 3-6 Table with Column Widths Set"

Set Alignment in Table Cells

Another customization is to set the alignment of the data in the table cells. You implement the logic in a new class named FormattedTableCellFactory and then set the alignment in the <TableColumn> markup in the FXML code.

  1. In NetBeans IDE, right-click the fxmltableview folder under Source Packages, and choose New then Java Class.

  2. Name the class FormattedTableCellFactory and then click Finish.

  3. Modify the FormattedTableCellFactory class by implementing the Callback class and creating instances of the TextAlignment and Format classes, as shown in Example 3-11. The S parameter is the type of the TableView generic type and the T parameter is the type of the content of all cells in this table column.

    Example 3-11 Callback Class

    public class FormattedTableCellFactory<S, T> 
        implements Callback<TableColumn<S, T>, TableCell<S, T>> {
        private TextAlignment alignment;
        private Format format;
        public TextAlignment getAlignment() {
            return alignment;
        public void setAlignment(TextAlignment alignment) {
            this.alignment = alignment;
        public Format getFormat() {
            return format;
        public void setFormat(Format format) {
            this.format = format;
  4. Implement the TableCell and TableColumn classes by appending the code in Example 3-12. This code overrides the updateItem method of the TableCell class and calls the setTextAlignment method on the table cell.

    Example 3-12 TableCell and TableColumn Classes

        public TableCell<S, T> call(TableColumn<S, T> p) {
            TableCell<S, T> cell = new TableCell<S, T>() {
                public void updateItem(Object item, boolean empty) {
                    if (item == getItem()) {
                    super.updateItem((T) item, empty);
                    if (item == null) {
                    } else if (format != null) {
                    } else if (item instanceof Node) {
                        super.setGraphic((Node) item);
                    } else {
            switch (alignment) {
                case CENTER:
                case RIGHT:
            return cell;
  5. Correct the import statements.

  6. In the fxml_tableview.fxml file, add the following code under the <cellValueFactory> markup to provide a center alignment for the First Name column, as shown in Example 3-13.

    Example 3-13 Alignment in Data Cell

    <TableColumn fx:id="firstNameColumn" text="First Name" prefWidth="100">
         <cellValueFactory><PropertyValueFactory property="firstName" />
              <FormattedTableCellFactory alignment="center">

    You can create an alignment for the remaining columns using left, right, or center values.

Running the application now results in data that is aligned in the center of the First Name column, as shown in Figure 3-7.

Figure 3-7 Data Center-Aligned in First Name Column

Description of Figure 3-7 follows
Description of "Figure 3-7 Data Center-Aligned in First Name Column"

Add Rows to the Table

You can add the ability for users to add a row of data to the table. Add the application logic in the FXMLTableViewController class. Then, modify the user interface to include three text fields and a button for entering the data.

  1. Open the file.

  2. Edit the FXMLTableViewController class so it looks like the code in Example 3-14.

    Example 3-14

    public class FXMLTableViewController {
        @FXML private TableView<Person> tableView;
        @FXML private TextField firstNameField;
        @FXML private TextField lastNameField;
        @FXML private TextField emailField;
        protected void addPerson(ActionEvent event) {
            ObservableList<Person> data = tableView.getItems();
            data.add(new Person(firstNameField.getText(),
  3. Correct the import statements, as shown in Example 3-15.

    Example 3-15 Import Statements in FXMLTableViewController

    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.scene.control.TableView;
    import javafx.scene.control.TextField;
  4. In the fxml_tableview.fxml file, add the following code before the </GridPane> markup, as shown in Example 3-16.

    Example 3-16 Text Fields and Button for Adding a Row

         <HBox spacing="10" alignment="bottom_right" GridPane.columnIndex="0" 
              <TextField fx:id="firstNameField" promptText="First Name"
              <TextField fx:id="lastNameField" promptText="Last Name"
              <TextField fx:id="emailField" promptText="email"
              <Button text="Add" onAction="#addPerson"/>

Run the application and you will see that the text fields and button appear below the table, as shown in Figure 3-8. Enter data in the text fields and click Add to see the application in action.

Download the file to see the completed source code of the FXMLTableView application.

Figure 3-8 Table with Text Fields and Button for Adding Data

Description of Figure 3-8 follows
Description of "Figure 3-8 Table with Text Fields and Button for Adding Data"

Where to Go from Here

This concludes the Address Book tutorial, but here are some things for you to try next:

  • Provide a filter to verify that data was entered in the correct format.

  • Customize the table by applying a cascading style sheet to distinguish between empty and non-empty rows. See "Styling UI Controls with CSS" in JavaFX UI Controls for more information.

  • Enable editing of data in the table. See Editing Data in the Table in Using JavaFX UI Controls for pointers.

  • Look at the Introduction to FXML document, which provides more information on the elements that make up the FXML language. The document is included in the javafx.fxml package in the API documentation.

Close Window

Table of Contents

JavaFX: Mastering FXML

Expand | Collapse