简体   繁体   中英

How to fill javafx.TableView in FXML controller?

I want to fill TableView with columns in controller but I don't know why my solution doesn't work.

in fxml the element I want to fill, looks like this:

<right>
    <TableView id="datesTableView">
    </TableView>
</right>

In the controller:

public class ProjectController implements Initializable {

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    LocalDate first = p.getStartDate(); 
    LocalDate last = p.getLastDay(); 

    List<TableColumn> list = new LinkedList<>();
    while(!first.equals(last)) {
        TableColumn tb = new TableColumn();
        tb.setGraphic(factory.getRotated(FileUtil.convertDateToString(first))); 
        list.add(tb);
        first = first.plusDays(1);
    }
    datesTableView.getColumns().setAll(list);

The following tableview is to contain column with dates. In the loop I am adding subsequent dates to columns and then to list. I receive error when I try to add the list of columns to datesTableView which is the last line. Rest works as I want - I tested it.

The error I receive is:

Caused by: java.lang.NullPointerException
    at ganttchart.controller.ProjectController.initialize(ProjectController.java:89)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 63 more
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Root cannot be null

Line 89 is

datesTableView.getColumns().setAll(list);

You'd need a property defined for datesTableView. Use the @FXML annotation with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM