java - 我的表格 View 中的表格单元格为空。 JavaFX + 场景构建器

标签 java javafx tableview scenebuilder tablecolumn

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

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

    }
}

这是正常现象并且有效,因此我认为您无需担心这个问题。

这是 Controller 类。我认为问题可能出在哪里。

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<Table> TableID;
    @FXML
    TableColumn<Table, Integer> aPlayerID;
    @FXML
    TableColumn<Table, String> aLeague;
    @FXML
    TableColumn<Table, String> aName;
    private int aNumber = 1;
    SimpleStringProperty str = new SimpleStringProperty();
    public MainController() {
        str.set("Hello");
    }

    final ObservableList<Table> 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<Table, Integer>("bPlayerID"));
        aLeague.setCellValueFactory( new PropertyValueFactory<Table, String>("bLeague"));
        aName.setCellValueFactory( new PropertyValueFactory<Table, String>("bName"));

        TableID.setItems(data);

    }

}

这也是表格查看器所需的表格类

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

你们知道可能出了什么问题吗?或者建议我如何仅添加表格查看器,其代码仍然可以与场景构建器中的 fxml 文件的其余部分一起使用?

最佳答案

您的get方法的名称是错误的。根据PropertyValueFactory documentation ,如果传入属性名称“xyz”,属性值工厂会首先查找属于表行中对象的方法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 tutorial 中的大纲来定义表模型。 :

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

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

关于java - 我的表格 View 中的表格单元格为空。 JavaFX + 场景构建器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080442/

相关文章:

java - 在Android上使用mockito加速编译

canvas - 如何以不锯齿的方式绘制由多条线段组成的曲线

json - 如何使用 Swift 4.2 创建搜索栏全局搜索可编码 JSON 数组值?

java - 如何在 Eclipse PDE 中表达项目间依赖关系

java - 无法从 SQLite 检索数据

JavaFX 8 TableView 带有用于显示/隐藏密码的切换按钮

java - 如何检测用户键盘是否在 Java 中的 AZERTY 中?

ios - imageview 始终位于 tableview 中心

ios - 表格 View 选择新 View

java - React Native Expo eas build -p android java JDK 版本错误