JavaFX setCellValueFactory

JavaFX setCellValueFactory

我知道,这个问题已经被问过数百次了,我花了最后 3 个小时阅读了所有这些问题。我正在用 JavaFX 编写一个小应用程序(我以前也这样做过……)。除了将内容放入我的专栏之外,一切正常:

@FXML public void initialize() {
    exerciseColumn.setCellValueFactory(new PropertyValueFactory<Exercise, String>("name"));
}

这是我的初始化方法和这个:

public StringProperty nameProperty() {
    return name;
}

是 属性 Getter 对于 "StringProperty name"。真正有趣的是,该代码是我前一段时间制作的另一个项目的精确副本(当然,我对所有内容都进行了两次和三次检查),我今天再次编译并且运行良好。我读了文档,感觉有一半的 JavaFX 来找到解决方案,但它没有任何意义。如果您认为您需要更多代码来帮助我,那么我当然可以提供。

编辑:具有相同问题的另一个示例(至少我希望如此)

主要:

package test;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import test.model.Exercise;
import test.view.Controller;

public class Main extends Application {

private Stage primaryStage;
private AnchorPane pane;

private ObservableList<Exercise> activeSession = FXCollections.observableArrayList();

public Main() {
    activeSession.add(new Exercise("ednhzrd"));
}

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("Test");

    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/view.fxml"));
        pane = (AnchorPane) loader.load();
        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.show();

        Controller controller = new Controller();
        controller.setMainApp(this);
    } catch(Exception e) {
        e.printStackTrace();
    }
}
}

控制器:

package test.view;

import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import test.Main;
import test.model.Exercise;

public class Controller {
@FXML private TableView<Exercise> exerciseTable;
@FXML private TableColumn<Exercise, String> exerciseColumn;

private Main main;

public Controller() {}

@FXML
public void initialize() {
    exerciseColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
}

public void setMainApp(Main main) {
    this.main = main;
}
}

练习:

enter codepackage test.model;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Exercise {

private StringProperty name;

public Exercise(String name) {
    this.name = new SimpleStringProperty(name);
}

public StringProperty nameProperty() {
    return name;
}
}

fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane prefHeight="400.0" prefWidth="209.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.view.Controller">
   <children>
      <TableView fx:id="exerciseTable" layoutX="200.0" layoutY="100.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columns>
          <TableColumn fx:id="exerciseColumn" prefWidth="75.0" text="C1" />
        </columns>
         <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
         </columnResizePolicy>
      </TableView>
   </children>
</AnchorPane>

您的 cellValueFactory 没有问题。这里唯一的问题是您从未在 table 中放置任何项目。如果修改controller在table中添加测试项:

package test.view;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import test.Main;
import test.model.Exercise;

public class Controller {
    @FXML
    private TableView<Exercise> exerciseTable;
    @FXML
    private TableColumn<Exercise, String> exerciseColumn;

    private ObservableList<Exercise> activeSession = FXCollections.observableArrayList();

    private Main main;

    public Controller() {
    }

    @FXML
    public void initialize() {
        exerciseColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
        activeSession.add(new Exercise("hrrykane"));
        exerciseTable.setItems(activeSession);
    }

    public void setMainApp(Main main) {
        this.main = main;
    }
}

然后你就看到了想要的效果: