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

时间:2014-11-22 17:42:27

标签: 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);

    }
}

这是正常的并且有效,所以我认为你不必担心那个。

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

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

    }

}

这里也是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);
    }
}

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

2 个答案:

答案 0 :(得分:3)

get方法的名称错误。根据{{​​3}},如果传入属性名称&#34; xyz&#34;,属性值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文档中所述,本案例中的属性不会是&#34; live&#34;:换句话说,如果值发生更改,则表格不会自动更新。此外,如果您想使表格可编辑,则不会在没有明确布线的情况下更新属性以调用set方法。

使用PropertyValueFactory documentation中的大纲定义表格模型更好:

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

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

答案 1 :(得分:-1)

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