为什么会赢得textfieldtablecells显示文本?

发布于 2025-02-01 20:23:10 字数 3605 浏览 2 评论 0原文

我目前正在Javafx进行一个项目,我当前的目标是创建一个TextfieldTableCells的表。团队对象携带一系列对象,并且该程序显示未添加TextFieldTableCells时所需的所有值。另一方面,如果将所有添加值设置为null,则表将包含可以正常工作的文本字段。当两者合并在一起时,问题就在于导致许多错误,这是在初始化包含表的Javafx类时发生的。为什么这不起作用?

    @FXML
    TableView table;
    @FXML
    TableColumn <Team, String> teamName = new TableColumn<>("Team Name:");
    @FXML
    TableColumn <Team, String> offRtg = new TableColumn<>("Offensive Ratings:");
    @FXML
    TableColumn <Team, String> defRtg = new TableColumn<>("Defensive Ratings");
    String[] teamNames = new String[20];
    String[] offRtgs = new String[20];
    String[] defRtgs = new String[20];
    int[] finalOffRtgs = new int[20];
    int[] finalDefRtgs = new int[20];
    Team[] teams = new Team[20];
    ObservableList<Team> allTeams;
    @FXML
    protected void toMain() {
        Driver.back();
    }
    @FXML
    public void initialize() {
        Driver.database.load();
        teams = Driver.database.getTeams();
        allTeams = FXCollections.observableArrayList(teams);
        teamName.setCellValueFactory(new PropertyValueFactory<>("teamName"));
        teamName.setStyle( "-fx-alignment: CENTER;");
        offRtg.setCellValueFactory(new PropertyValueFactory<>("offRtg"));
        offRtg.setStyle( "-fx-alignment: CENTER;");
        defRtg.setCellValueFactory(new PropertyValueFactory<>("defRtg"));
        defRtg.setStyle( "-fx-alignment: CENTER;");
        table.setEditable(true);
        teamName.setEditable(true);
        offRtg.setEditable(true);
        defRtg.setEditable(true);
        teamName.setCellFactory(TextFieldTableCell.forTableColumn());
        offRtg.setCellFactory(TextFieldTableCell.forTableColumn());
        defRtg.setCellFactory(TextFieldTableCell.forTableColumn());
        for (int x = 0; x < 20; x++) {
            table.getItems().add(allTeams.get(x));

        }

    }
}

所有tablecolumns都在表对象内部,这是在SceneBuilder中完成的,这就是为什么它在此类中不显示的原因。同样,该表可与Textfields或值一起使用,但并非两者兼而有之。我以前从未遇到过这个问题。 这是FXML类:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="780.0" prefWidth="661.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.football_sim.EditTeams">
   <children>
      <Button layoutX="282.0" layoutY="754.0" mnemonicParsing="false" onAction="#toMain" text="Return to Main" />
      <Label alignment="CENTER" layoutX="-1.0" layoutY="-1.0" prefHeight="72.0" prefWidth="661.0" text="Edit Teams" textAlignment="CENTER">
         <font>
            <Font size="41.0" />
         </font>
      </Label>
      <TableView fx:id="table" editable="true" layoutX="105.0" layoutY="91.0" prefHeight="524.0" prefWidth="451.0">
        <columns>
          <TableColumn fx:id="teamName" maxWidth="150.0" minWidth="150.0" prefWidth="150.0" sortable="false" text="Team Name" />
          <TableColumn fx:id="offRtg" maxWidth="150.0" minWidth="150.0" prefWidth="150.0" sortable="false" text="OffRtg" />
            <TableColumn fx:id="defRtg" maxWidth="150.0" minWidth="150.0" prefWidth="150.0" sortable="false" text="DefRtg" />
        </columns>
      </TableView>
   </children>
</AnchorPane>

I am currently making a project in JavaFX, where my current goal is to create a table of TextFieldTableCells. A Team object carries a series of objects, and the program displays all of the values it needs to when the TextFieldTableCells are not added. On the other hand, if all of the added values are set to null, the table will contain textFields which work just fine. The problem lies when both are combined together, which causes many errors, which happen when the JavaFX class containing the table is initialized. Why doesn't this work?

    @FXML
    TableView table;
    @FXML
    TableColumn <Team, String> teamName = new TableColumn<>("Team Name:");
    @FXML
    TableColumn <Team, String> offRtg = new TableColumn<>("Offensive Ratings:");
    @FXML
    TableColumn <Team, String> defRtg = new TableColumn<>("Defensive Ratings");
    String[] teamNames = new String[20];
    String[] offRtgs = new String[20];
    String[] defRtgs = new String[20];
    int[] finalOffRtgs = new int[20];
    int[] finalDefRtgs = new int[20];
    Team[] teams = new Team[20];
    ObservableList<Team> allTeams;
    @FXML
    protected void toMain() {
        Driver.back();
    }
    @FXML
    public void initialize() {
        Driver.database.load();
        teams = Driver.database.getTeams();
        allTeams = FXCollections.observableArrayList(teams);
        teamName.setCellValueFactory(new PropertyValueFactory<>("teamName"));
        teamName.setStyle( "-fx-alignment: CENTER;");
        offRtg.setCellValueFactory(new PropertyValueFactory<>("offRtg"));
        offRtg.setStyle( "-fx-alignment: CENTER;");
        defRtg.setCellValueFactory(new PropertyValueFactory<>("defRtg"));
        defRtg.setStyle( "-fx-alignment: CENTER;");
        table.setEditable(true);
        teamName.setEditable(true);
        offRtg.setEditable(true);
        defRtg.setEditable(true);
        teamName.setCellFactory(TextFieldTableCell.forTableColumn());
        offRtg.setCellFactory(TextFieldTableCell.forTableColumn());
        defRtg.setCellFactory(TextFieldTableCell.forTableColumn());
        for (int x = 0; x < 20; x++) {
            table.getItems().add(allTeams.get(x));

        }

    }
}

All TableColumns are inside the table object, this has been done in SceneBuilder which is why it doesn't show up in this class. Again, the table works with TextFields or with the values, but not both. I've never encountered this problem before.
This is the fxml class:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="780.0" prefWidth="661.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.football_sim.EditTeams">
   <children>
      <Button layoutX="282.0" layoutY="754.0" mnemonicParsing="false" onAction="#toMain" text="Return to Main" />
      <Label alignment="CENTER" layoutX="-1.0" layoutY="-1.0" prefHeight="72.0" prefWidth="661.0" text="Edit Teams" textAlignment="CENTER">
         <font>
            <Font size="41.0" />
         </font>
      </Label>
      <TableView fx:id="table" editable="true" layoutX="105.0" layoutY="91.0" prefHeight="524.0" prefWidth="451.0">
        <columns>
          <TableColumn fx:id="teamName" maxWidth="150.0" minWidth="150.0" prefWidth="150.0" sortable="false" text="Team Name" />
          <TableColumn fx:id="offRtg" maxWidth="150.0" minWidth="150.0" prefWidth="150.0" sortable="false" text="OffRtg" />
            <TableColumn fx:id="defRtg" maxWidth="150.0" minWidth="150.0" prefWidth="150.0" sortable="false" text="DefRtg" />
        </columns>
      </TableView>
   </children>
</AnchorPane>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文