我的表格视图中的表格单元为空。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<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);

}

}

你们知道什么地方可能出错,或者建议我如何只添加tableviewer,使其代码仍可与SceneBuilder中的其余fxml文件一起使用?

回答:

您的get方法名称错误。根据PropertyValueFactory文档,如果传入属性名称“

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方法的情况下,它不会更新属性。

最好使用“ 属性和绑定”教程中的大纲定义表模型:

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

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

以上是 我的表格视图中的表格单元为空。JavaFX + Scenebuilder 的全部内容, 来源链接: utcz.com/qa/398446.html

回到顶部