【问题标题】:The table cells are empty in my tableview. JavaFX + Scenebuilder我的表格视图中的表格单元格是空的。 JavaFX + 场景构建器
【发布时间】:2015-01-20 17:24:22
【问题描述】:

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

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

你们知道可能出了什么问题,或者建议我如何只添加 tableviewer 的代码,该代码仍然适用于 scenebuilder 中的 fxml 文件的其余部分吗?

【问题讨论】:

    标签: java javafx tableview scenebuilder tablecolumn


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!然而,这仅适用于 intergerproperty。我尝试了 StringProperty,但由于某种原因它不起作用。字符串单元格仍然是空的
    • 你对其他方法做了同样的改变?
    • 是的,它现在看起来像这样:我刚刚注意到我无法在评论中显示代码 public StringProperty getbLeague() { return bLeague; }
    • 你需要getBLeague() { ...}
    • 什么意思? public StringProperty getbLeague() { return bLeague; } 你的意思是它应该是 getBLeague 而不是 getbLeague?好吧,我刚刚明白了。 bLeagueProperty()。再次感谢你。我已经坐了大概 6 个小时了,现在还没有弄清楚。为什么它应该被命名为 bNameProperty?我见过的教程都没有使用这个。
    【解决方案2】:

    在 stringproperty 中,您必须在 cellData.getValue().bPlayerIDProperty()) 之后添加 asobjects() 方法...所以我希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 2020-01-03
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多