表格单元格在我的tableview中是空的。 JavaFX + Scenebuilder

我正在尝试让表格单元格在我创建新行时显示字符串。 但是所有的行都是空的。 谁知道我做错了什么? 这是主要的类:包应用程序;

import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Cursor; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("/fxml/BasketCase_GUI_0.3.fxml")); Scene scene = new Scene(root,1110,740); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); primaryStage.setResizable(false); primaryStage.setScene(scene); primaryStage.setTitle("Basket Case_Beta"); primaryStage.show(); scene.setCursor(Cursor.DEFAULT); } public static void main(String[] args) throws Exception { launch(args); } } 

这是正常的和工作,所以我不认为你需要担心那一个。

这是控制器类。 我认为问题可能在哪里。

 package application; import java.net.URL; import java.util.ResourceBundle; import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; 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 MainController implements Initializable { @FXML TableView TableID; @FXML TableColumn
aPlayerID; @FXML TableColumn
aLeague; @FXML TableColumn
aName; private int aNumber = 1; SimpleStringProperty str = new SimpleStringProperty(); public MainController() { str.set("Hello"); } final ObservableList
data = FXCollections.observableArrayList( new Table(aNumber++, "hehe", "hoho"), new Table(aNumber++, "hehe", "hoho"), new Table(aNumber++, "hehe", "hoho") ); public void buttonClick(ActionEvent event) { data.add(new Table(aNumber++, "hehe", "hoho")); TableID.getColumns().addAll(aPlayerID, aLeague, aName); } @Override public void initialize(URL arg0, ResourceBundle arg1) { aPlayerID.setCellValueFactory( new PropertyValueFactory
("bPlayerID")); aLeague.setCellValueFactory( new PropertyValueFactory
("bLeague")); aName.setCellValueFactory( new PropertyValueFactory
("bName")); TableID.setItems(data); } }

这里也是tableviewer所需的表类

 package application; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; public class Table { private final SimpleIntegerProperty bPlayerID; private final SimpleStringProperty bLeague; private final SimpleStringProperty bName; public Table(int cPlayerID, String cLeague, String cName) { this.bPlayerID = new SimpleIntegerProperty(cPlayerID); this.bLeague = new SimpleStringProperty(cLeague); this.bName = new SimpleStringProperty(cName); } public int getbPlayerID() { return bPlayerID.get(); } public void setbPlayerID(int v) { bPlayerID.set(v); } public String getbLeague() { return bLeague.get(); } public void setbLeague(String v) { bLeague.set(v); } public String getbName() { return bName.get(); } public void setbName(String v) { bName.set(v); } } 

你们知道可能出现什么问题吗?或者可能会建议我如何只使用仍然可以与scenebuilder的其余fxml文件一起使用的代码添加tableviewer?

get方法的名称是错误的。 根据PropertyValueFactory文档 ,如果传入属性名称“xyz”,属性值factory将首先查找属于表行中对象的方法xyzProperty() 。 如果它没有找到,它将回退寻找一个名为getXyz()的方法(仔细查看那里的大写),将结果包装在ReadOnlyObjectWrapper

所以以下方法可行:

 package application; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; public class Table { private final SimpleIntegerProperty bPlayerID; private final SimpleStringProperty bLeague; private final SimpleStringProperty bName; public Table(int cPlayerID, String cLeague, String cName) { this.bPlayerID = new SimpleIntegerProperty(cPlayerID); this.bLeague = new SimpleStringProperty(cLeague); this.bName = new SimpleStringProperty(cName); } public int getBPlayerID() { return bPlayerID.get(); } public void setBPlayerID(int v) { bPlayerID.set(v); } public String getBLeague() { return bLeague.get(); } public void setBLeague(String v) { bLeague.set(v); } public String getBName() { return bName.get(); } public void setBName(String v) { bName.set(v); } } 

但是,如PropertyValueFactory文档中所述,此情况下的属性不会“生效”:换句话说,如果值更改,则表不会自动更新。 此外,如果您想使表格可编辑,则在没有显式连线调用set方法的情况下,它不会更新属性。

最好使用Properties and Bindings教程中的大纲来定义表模型:

 package application; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; public class Table { private final IntegerProperty bPlayerID; private final StringProperty bLeague; private final StringProperty bName; public Table(int cPlayerID, String cLeague, String cName) { this.bPlayerID = new SimpleIntegerProperty(cPlayerID); this.bLeague = new SimpleStringProperty(cLeague); this.bName = new SimpleStringProperty(cName); } public int getBPlayerID() { return bPlayerID.get(); } public void setBPlayerID(int v) { bPlayerID.set(v); } public IntegerProperty bPlayerIDProperty() { return bPlayerID ; } public String getBLeague() { return bLeague.get(); } public void setBLeague(String v) { bLeague.set(v); } public StringProperty bLeagueProperty() { return bLeague ; } public String getBName() { return bName.get(); } public void setBName(String v) { bName.set(v); } public StringProperty bNameProperty() { return bName ; } } 

如果你这样做,那么(在Java 8中)你可以使用以下单元格值工厂,而不是PropertyValueFactory

 aPlayerID.setCellValueFactory(cellData -> cellData.getValue().bPlayerIDProperty()); 

这将允许编译器捕获任何错误,而不是它只是在运行时静默失败。

在stringproperty你需要在cellData.getValue()之后添加asobjects()方法.bPlayerIDProperty())…所以我希望这会有所帮助