当前位置: 首页 > 面试题库 >

我的表格视图中的表格单元为空。JavaFX + Scenebuilder

周辉
2023-03-14
问题内容

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

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

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



 类似资料:
  • 我希望我的swift代码在每次按下按钮时都添加一个新的tableview单元格。你可以在下面的gif中看到我想要的东西。这段代码应该在func-tableView(_tableView:UITableView,cellForRowAt-indexPath:indexPath)中添加按钮- 在此输入图像描述

  • 问题内容: 我正在尝试为在表格视图单元格中按下的按钮运行一个动作。下面的代码在我的表视图控制器类中。 在我的UITableViewCell类的称为requestCell的插座中,该按钮已被描述为“是”。 我正在使用“解析”来保存数据,并且想在按下按钮时更新对象。我的objectIds数组可以正常工作,cell.yes.tag还会在日志中输出正确的数字,但是,为了正常运行查询,我无法将该数字输入“连

  • 我的TableView包含一些数字数据。当我说按按钮编辑值时,它会将单元格的背景更改为绿色。当没有足够的行使表格可滚动时,这种方法很有效。一旦表格变成(或从一开始就是)可滚动的,它就会开始表现出怪异的行为。它会更改已编辑项目的背景,但也会更改在向下滚动之前不可见的项目的背景。然而,当我调试时,更改背景的代码只被调用一次。我认为它与tableview有关,当is scrollable破坏并重新创建单

  • 问题内容: 我正在尝试用Java编写程序来管理我的Bookie帐户。我是java的新手,所以我认为我会选择一些简单的方法来了解事情的原理。我决定使用表格视图并使各个单元格可编辑。我一直在关注本教程http://java- buddy.blogspot.co.uk/2012/04/javafx-2-editable-tableview.html 。它详细说明了如何使用Java代码执行此操作,并将其复

  • 我有一个带有表视图的Xib(我使用的是Xib而不是常规的故事板,因为我将Xib插入到UIPageView控制器中)。在 Xib 的类中,我注册了一个自定义表视图单元格。尽管单元格插入到表视图中,但它不会根据其约束正确调整大小,也不会更改表视图的显示。 这是我在视图控制器中的设置代码,它具有表视图xib:类 当我运行应用程序时,它看起来像这样: 所以基本上,自定义单元格是从xib文件添加到表格视图中