FXML文件不接受属性值或可观察列表。也许控制器类配置不正确?

时间:2017-07-18 01:16:05

标签: java javafx fxml scenebuilder

我正在尝试通过制作一个简单的食谱应用程序来学习使用scenebuilder的JavaFX,并且在确定如何将属性和可观察列表注入FXML tableview及其相关列时遇到困难。下面是我的简单java类和fXML文件。

FXML控制器类:

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);
        }

    }

和FXML文件:

<?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>

0 个答案:

没有答案