Ayoub.A
Ayoub.A

Reputation: 2093

FXML: Bind TableViews items property to controller

I have a TableView in FXML, and I want to bind it's items property to a list in the controller (just like in WPF), my FXML code is like this:

<TableView fx:controller="controllers.MyController"  items="$controller.loggings">
    <columns>
        <TableColumn text="Nom">
            <cellValueFactory>
                <PropertyValueFactory property="name" />
            </cellValueFactory>
        </TableColumn>

        <TableColumn text="Type">
            <cellValueFactory>
                <PropertyValueFactory property="type" />
            </cellValueFactory>
        </TableColumn>
    </columns>
</TableView>

and my controller is as follows:

public class MyController {
    public ObservableList<Logging> loggings = FXCollections.observableArrayList();

    @FXML
    protected void initialize(){
        loggings.add(new Logging(){{
            setName("hilton");
            setType("hotel");

        }});
    }
}

For some reason an Exception launches. What am I doing wrong?

Upvotes: 0

Views: 1157

Answers (2)

fabian
fabian

Reputation: 82461

You haven't declared the fx namespace:

<TableView xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.MyController"  items="$controller.loggings">
    ...
</TableView>

Furthermore the controller class needs to contain a getter for the loggings field:

private final ObservableList<Logging> loggings = FXCollections.observableArrayList();

public ObservableList<Logging> getLoggings() {
    return loggings;
}

Note: JavaFX does not consider fields for expression binding, therefore the getter is needed.

Upvotes: 1

fireandfuel
fireandfuel

Reputation: 732

Your FXML file is wrong. You have to assign the items to the TableView inside your controller class.

FXML file:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.cell.PropertyValueFactory?>

<TableView xmlns:fx="http://javafx.com/fxml" fx:controller="controllers.MyController" fx:id="tableView">
    <columns>
        <TableColumn text="Nom">
            <cellValueFactory>
                <PropertyValueFactory property="name" />
            </cellValueFactory>
        </TableColumn>

        <TableColumn text="Type">
            <cellValueFactory>
                <PropertyValueFactory property="type" />
            </cellValueFactory>
        </TableColumn>
    </columns>
</TableView>

Controller:

package controllers;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;

public class MyController {
    @FXML
    private TableView<Logging> tableView;

    public ObservableList<Logging> loggings = FXCollections.observableArrayList();

    @FXML
    protected void initialize(){
        loggings.add(new Logging(){{
            setName("hilton");
            setType("hotel");

        }});

        tableView.setItems(loggings);
    }
}

Upvotes: 0

Related Questions