我知道,这个问题已被问过几百次,我花了最后3个小时阅读了所有这些问题。我正在用JavaFX编写一个小应用程序(我之前已经这样做了......)。除了将内容放入我的专栏之外,一切正常:
@FXML public void initialize() {
exerciseColumn.setCellValueFactory(new PropertyValueFactory<Exercise, String>("name"));
}
这是我的初始化方法,并且:
public StringProperty nameProperty() {
return name;
}
是“StringProperty name”的Property Getter。真正有趣的是,代码是我前一段时间制作的另一个项目的精确副本(当然,我对所有内容进行了双倍和三倍检查),我今天再次编译,这是完美的。我阅读文档,因为它感觉,找到一个解决方案的一半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>
答案 0 :(得分:0)
cellValueFactory
没问题。这里唯一的问题是你永远不会在表格中放置任何项目。如果修改控制器以将测试项添加到表中:
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;
}
}
然后你会看到所需的效果: