I'm trying to learn JavaFX with scenebuilder by making a simple cookbook application and am having something of a difficult time with figuring out how to inject properties and observable lists into an FXML tableview and its associated columns. Below are my simple java classes and fXML file.
FXML controller class:
package application;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class FXMLTableViewController implements Initializable {
@FXML private TableView<Recipe> tableView;
@FXML private TableColumn<Recipe, String> colName;
private List <Recipe> recipeList;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
colName.setCellValueFactory(new PropertyValueFactory<Recipe, String>("RecipeName"));
Recipe rOne = new Recipe("Test Name");
Recipe rTwo = new Recipe("Test Name 2");
recipeList.add(rOne);
recipeList.add(rTwo);
tableView.getItems().setAll(recipeList);
}
}
Main.java
package application;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
try {
Parent root = FXMLLoader.load(getClass().getResource("/views/fxml_tableview.fxml"));
primaryStage.setTitle("Potluck");
primaryStage.setScene(new Scene(root, 600, 400));//scene, width, height
// navScene.getRoot().getChildrenUnmodifiable().add(findScene.getRoot());
primaryStage.setMinHeight(400);
primaryStage.setMinWidth(600);
primaryStage.show();
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
launch(args);
}
}
Recipe.java:
package application;
import javafx.beans.property.SimpleStringProperty;
public class Recipe {
private final SimpleStringProperty recipeName = new SimpleStringProperty("");
public Recipe() {
this("");
}
public Recipe(String recipeName) {
setRecipeName(recipeName);
}
public String getRecipeName() {
return recipeName.get();
}
public void setRecipeName(String rName) {
recipeName.set(rName);
}
}
And the FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.cell.*?>
<?import javafx.collections.*?>
<?import fxmltableview.*?>
<?import java.lang.String?>
<AnchorPane id="myAnchor" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FXMLTableViewController">
<children>
<TableView prefHeight="400.0" prefWidth="600.0" id="tableView">
<columns>
<TableColumn prefWidth="599.0" text="Recipe Name" fx:id="colName" />
</columns>
</TableView>
</children>
</AnchorPane>